코딩테스트-알고리즘/프로그래머스
[프로그래머스/Lv.0] 안전지대
닉네임생각즁
2024. 1. 29. 15:21
https://school.programmers.co.kr/learn/courses/30/lessons/120866
class Solution {
int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
int[] dy = {0, 1, 1, 1, 0, -1, -1, -1};
boolean[][] visited;
public int solution(int[][] board) {
int answer = 0;
visited = new boolean[board.length][board[0].length];
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == 1) {
check(i, j);
}
}
}
int count = 0;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(visited[i][j] == true) {
count++; // true 개수 세기
}
}
}
answer = (board.length * board.length) - count;
return answer;
}
void check(int x, int y) {
visited[x][y] = true; // 1인 곳 true 표시
for(int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=0 && nx<visited.length && ny>=0 && ny<visited.length) {
visited[nx][ny] = true; // 지뢰 근처 전부 true 표시
}
}
}
}
1을 발견하면 1 포함 그 주변을 true로 만들어주고 돌아오는걸 전체 배열을 돌며 반복한다
다 하고나면 true 개수를 세주고 전체 칸인 n*n에서 true 개수를 빼주면 안전지대의 칸 수가 나온다