Skip to content

Commit d31be3f

Browse files
committed
21736 헌내기는 친구가 필요해
1 parent 61734b6 commit d31be3f

File tree

2 files changed

+79
-75
lines changed

2 files changed

+79
-75
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package bfs.silver.silver_2;
2+
3+
import java.io.*;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.StringTokenizer;
7+
8+
public class _21736 {
9+
10+
private static int n;
11+
private static int m;
12+
13+
private static char[][] graph;
14+
private static boolean[][] visited;
15+
private static Queue<int[]> queue;
16+
17+
private static int[] dx = {-1, 1, 0, 0};
18+
private static int[] dy = {0, 0, -1, 1};
19+
20+
public static void main(String[] args) throws IOException {
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
StringBuilder sb = new StringBuilder();
25+
26+
n = Integer.parseInt(st.nextToken());
27+
m = Integer.parseInt(st.nextToken());
28+
29+
graph = new char[n][m];
30+
visited = new boolean[n][m];
31+
queue = new LinkedList<>();
32+
33+
for (int i = 0; i < n; i++) {
34+
String input = br.readLine();
35+
36+
for (int j = 0; j < m; j++) {
37+
graph[i][j] = input.charAt(j);
38+
39+
if (graph[i][j] == 'I') {
40+
queue.offer(new int[]{i, j});
41+
}
42+
}
43+
}
44+
45+
bw.write(bfs());
46+
bw.flush();
47+
bw.close();
48+
}
49+
50+
private static String bfs() {
51+
int count = 0;
52+
53+
while (!queue.isEmpty()) {
54+
int[] cur = queue.poll();
55+
56+
for (int i = 0; i < 4; i++) {
57+
int nx = cur[0] + dx[i];
58+
int ny = cur[1] + dy[i];
59+
60+
if (nx < 0 || ny < 0 || nx >= n || ny >= m || graph[nx][ny] == 'X') {
61+
continue;
62+
}
63+
64+
if (!visited[nx][ny]) {
65+
visited[nx][ny] = true;
66+
67+
if (graph[nx][ny] == 'P') {
68+
count++;
69+
}
70+
71+
queue.offer(new int[]{nx, ny});
72+
}
73+
}
74+
}
75+
76+
return count == 0 ? "TT" : String.valueOf(count);
77+
}
78+
79+
}

src/main/java/dfs/silver/silver_2/_21736.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)