Skip to content

Commit

Permalink
Merge pull request #172 from AlgoLeadMe/2-mjj111
Browse files Browse the repository at this point in the history
2-mjj111
  • Loading branch information
mjj111 authored May 31, 2024
2 parents 66bb170 + 3053a63 commit 91d98fd
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions mjj111/거리두기_확인하기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;

class Solution {
public static int[] solution(String[][] places) {
int[] answer = new int[places.length];

for (int i = 0; i < places.length; i++) {
String[] p = places[i];

boolean isOk = true;
for (int r = 0; r < 5 && isOk; r++) {
for (int c = 0; c < 5 && isOk; c++) {
if (p[r].charAt(c) == 'P') {
if (!bfs(r, c, p))
isOk = false;
}
}
}
answer[i] = isOk ? 1 : 0;
}

return answer;
}

private static boolean bfs(int r, int c, String[] p) {
int dr[] = { -1, 1, 0, 0 };
int dc[] = { 0, 0, -1, 1 };

Queue<int[]> queue = new LinkedList<int[]>();
queue.offer(new int[] { r, c });

while (!queue.isEmpty()) {
int[] position = queue.poll();

for (int i = 0; i < 4; i++) {
int nr = position[0] + dr[i];
int nc = position[1] + dc[i];

if (nr < 0 || nc < 0 || nr >= 5 || nc >= 5 || (nr == r && nc == c))
continue;

int d = Math.abs(nr - r) + Math.abs(nc - c);

if (p[nr].charAt(nc) == 'P' && d <= 2)
return false;
if (p[nr].charAt(nc) == 'O' && d < 2)
queue.offer(new int[] { nr, nc });
}
}

return true;
}
}

0 comments on commit 91d98fd

Please sign in to comment.