https://school.programmers.co.kr/learn/courses/30/lessons/120866
class Solution {
public int solution(int[][] board) {
int answer = 0;
int[] dx = {-1, 0, 1, 0, -1, 1, -1, 1};
int[] dy = {0, 1, 0, -1, -1, -1, 1, 1};
int length = board.length;
boolean[][] checkBoard = new boolean[length][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
boolean flag = true;
for (int k = 0; k < 8; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (board[i][j] == 1) {
checkBoard[i][j] = true;
}
if (nx >= 0 && nx < length && ny >= 0 && ny < length && board[nx][ny] == 1) {
checkBoard[i][j] = true;
}
}
}
}
for (int i = 0; i < checkBoard.length; i++) {
for (int j = 0; j < checkBoard.length; j++) {
if (!checkBoard[i][j]) {
answer++;
}
}
}
return answer;
}
}
dfs라고한다.
'내일배움캠프 > TIL' 카테고리의 다른 글
2022.12.16 (0) | 2022.12.16 |
---|---|
2022.12.15 (0) | 2022.12.15 |
2022.12.12 (0) | 2022.12.12 |
2022.12.9 (0) | 2022.12.09 |
2022.12.7 (0) | 2022.12.07 |