package 프로그래머스.자바.거리두기확인하기;
class Solution {
int[] dx = {0, 0, -1, 1};
int[] dy = {1, -1, 0, 0};
char[][] arr = new char[5][5];
boolean[][] visited = new boolean[5][5];
boolean isNotDistancing;
void dfs(int x, int y, int depth){
if(isNotDistancing) return;
if(depth > 1) return;
visited[x][y] = true;
for(int i =0; i < 4; i++){
int nx = dx[i] + x;
int ny = dy[i] + y;
if(nx < 0 || ny < 0 || nx >= 5 || ny >= 5 || visited[nx][ny]) continue;
if(arr[nx][ny] == 'O') {
dfs(nx, ny, depth+1);
}
else if(arr[nx][ny] == 'P') isNotDistancing = true;
}
}
public int[] solution(String[][] places) {
int[] answer = new int[5];
for(int i = 0; i < 5; i++){
for(int a = 0; a < 5; a++){
String s = places[i][a];
for(int b = 0; b < 5; b++){
arr[a][b] = s.charAt(b);
}
}
isNotDistancing = false;
for(int a = 0; a < 5; a++){
for(int b = 0; b < 5; b++){
if(arr[a][b] == 'P' && !isNotDistancing){
dfs(a, b, 0);
}
}
visited = new boolean[5][5];
}
if(isNotDistancing) answer[i] = 0;
else answer[i] = 1;
}
return answer;
}
}
'문제풀이 > 프로그래머스' 카테고리의 다른 글
다단계 칫솔 판매 Java (0) | 2024.02.25 |
---|---|
행렬 테두리 회전하기 Java (0) | 2024.02.23 |
[PCCP 기출문제 2번] 석유 시추 (0) | 2024.02.22 |
[PCCP 기출문제 1번] 붕대 감기 (0) | 2024.02.21 |
[2021 카카오 채용연계형 인턴십] 숫자 문자열과 영단어 (0) | 2024.01.24 |