์ฒด๊ฐ Level : โ
โ
โ
Review: ์๋ก์ด ๋ฐฐ์ด์ ์ ๊ทผ ๋ฐฉ๋ฒ์ ๋ํด ๋ฐฐ์ธ ์ ์์๋ค. ์ ๋ฐ์ ์ธ ์ ๊ทผ๋ฒ์ ๋ง์์ง๋ง ์ธ๋ถ์ ์ธ ์กฐ๊ฑด๋ค์ด ํ๋ ค์ ๊ณ ์ํ๋ค ใ |
๐ก ์ง๋ขฐ๊ฐ ๋งค์ค๋ ์ง์ญ์ ์ง๋ board๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์์ ํ ์ง์ญ์ ์นธ ์๋ฅผ return
1. ์ํ์ง์ญ์ ํ์ํด์ค ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด์ค๋ค ํฌ๊ธฐ๋ ๊ธฐ์กด๋ฐฐ์ด์ +2
(board ํฌ๊ธฐ์ ๊ฐ์ ๋ฐฐ์ด์ ๋ง๋ค๋ฉด ์ํ์ง์ญ์ ํ์ํ ๋ ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ๋ฏ๋ก +2 +2 ํด์ค๋ค)
2. board ์ ๋๋ ค๊ฐ๋ฉฐ ์ง๋ขฐ๊ฐ ๋์ค๋ฉด
์๋ก์ด ๋ฐฐ์ด์ ์ง๋ขฐ๊ฐ ๋์จ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก +1 +2 ํ์ฌ ์ํ์ง์ญ์ ํ์ํด์ค๋ค.
3. ์ดํ ๊ธฐ์กด ๋ฐฐ์ด ๋ฒ์๊น์ง์ 0์ ์นด์ดํธํด์ฃผ๋ฉด ๋๋ค!
class Solution {
public int solution(int[][] board) {
int answer = 0;
int[][] tmp = new int[board.length+2][board.length+2];
for(int i =0 ; i< board.length; i++){
for (int j = 0; j < board.length; j++){
if (board[i][j] == 1){
tmp[i][j+1] = 1;
tmp[i][j] = 1;
tmp[i][j+2] = 1;
tmp[i+1][j+1] = 1;
tmp[i+1][j] = 1;
tmp[i+1][j+2] = 1;
tmp[i+2][j+1] = 1;
tmp[i+2][j] = 1;
tmp[i+2][j+2] = 1;
}
}
}
for(int i =1 ; i< tmp.length-1; i++){
for (int j = 1; j < tmp.length-1; j++){
if (tmp[i][j] == 0){
answer++;
}
}
}
return answer;
}
}
[์คํจ] 1์ด ์ฌ๋ฌ๊ฐ ๋์ฌ๋ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํด์ผํ๋ค.
๋ฐฐ์ด์ ๋ด์๊น ์ถ๊ธฐ๋ ํ๋๋ฐ ๋๋ฌด ๋ณต์กํด์ง///////
๋ฐฐ์ด ๋ฒ์ ๋๋ ๊ฒฝ์ฐ ๋๋ฌธ์ ๋ฐฐ์ด์ 7,7๋ก ๋ํ์ ์ ๊ทผํ๊ณ
์ถ๋ ฅํ ๋ ์ค์ 5,5 ๋ฒ์๋ง ๋น๊ตํ๊ธฐ.
class Solution {
public int solution(int[][] board) {
int answer = 0;
int[][] tmp = new int[7][7];
int j_tmp = 0;
int i_tmp = 0;
int flag = 0;
for(int i =0 ; i< board.length; i++){
for (int j = 0; j < board[0].length; j++){
if (board[i][j] == 1){
j_tmp = j; i_tmp = i;
if (flag != 25){
flag++;
}
}
}
}
if (flag != 25){
for(int i =0 ; i< tmp.length; i++){
for (int j = 0; j < tmp[0].length; j++){
tmp[j_tmp][i_tmp+1] = 1;
tmp[j_tmp][i_tmp] = 1;
tmp[j_tmp][i_tmp-1] = 1;
tmp[j_tmp+1][i_tmp+1] = 1;
tmp[j_tmp+1][i_tmp] = 1;
tmp[j_tmp+1][i_tmp-1] = 1;
tmp[j_tmp-1][i_tmp+1] = 1;
tmp[j_tmp-1][i_tmp] = 1;
tmp[j_tmp-1][i_tmp-1] = 1;
}
}
/*for(int i = i_tmp-1; i <= i_tmp+1; i++){
for (int j = j_tmp-1; j <= j_tmp+1; j++){
tmp[i][j] = 1;
}
} */
for(int i =1 ; i< tmp.length-1; i++){
for (int j = 1; j < tmp.length-1; j++){
if (tmp[i][j] == 1){
answer++;
}
}
}
}
return answer;
}
}