Skip to content

Commit f931dbf

Browse files
committed
[Simulation] baekjoon-2636
1 parent 3e7e5dc commit f931dbf

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
| 19 | | [Baekjoon-2931 ๊ฐ€์Šค๊ด€](./src/Simulation/P2931) | |
5353
| 20 | | [JUNGOL-1205 ์กฐ์ปค](./src/Simulation/jo1205) | |
5454
| 21 | | [SWEA-1767 ํ”„๋กœ์„ธ์„œ ์—ฐ๊ฒฐํ•˜๊ธฐ](./src/Simulation/swea1767) | |
55+
| 22 | | [Baekjoon-2636 ์น˜์ฆˆ](./src/Simulation/P2636) | |
5556

5657
## String
5758

โ€Žsrc/Simulation/P2636/Main.java

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package Simulation.P2636;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int R, C, total;
9+
static int[][] map;
10+
static int[] di = {-1, 0, 1, 0};
11+
static int[] dj = {0, 1, 0, -1};
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Simulation/P2636/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
R = Integer.parseInt(st.nextToken());
19+
C = Integer.parseInt(st.nextToken());
20+
21+
map = new int[R][C];
22+
for (int i = 0; i < R; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
for (int j = 0; j < C; j++) {
25+
map[i][j] = Integer.parseInt(st.nextToken());
26+
if (map[i][j] == 1) total ++;
27+
}
28+
}
29+
30+
solve();
31+
}
32+
33+
static void solve() {
34+
Queue<Pos> air = new LinkedList<>();
35+
Queue<Pos> cheese = new LinkedList<>();
36+
boolean[][] visited = new boolean[R][C];
37+
int time = 0;
38+
39+
air.add(new Pos(0, 0));
40+
while (total > 0) {
41+
time ++;
42+
43+
while (!air.isEmpty()) {
44+
Pos p = air.poll();
45+
visited[p.i][p.j] = true;
46+
47+
for (int k = 0; k < 4; k++) {
48+
int to_i = p.i + di[k];
49+
int to_j = p.j + dj[k];
50+
51+
if (!isValidPath(to_i, to_j) || visited[to_i][to_j]) continue;
52+
53+
visited[to_i][to_j] = true;
54+
if (map[to_i][to_j] == 0) {
55+
air.offer(new Pos(to_i, to_j));
56+
} else {
57+
cheese.offer(new Pos(to_i, to_j));
58+
total --;
59+
}
60+
}
61+
}
62+
63+
air.addAll(cheese);
64+
cheese.clear();
65+
}
66+
67+
System.out.println(time);
68+
System.out.println(air.size());
69+
}
70+
71+
static boolean isValidPath(int i, int j) {
72+
return 0 <= i && i < R && 0 <= j && j < C;
73+
}
74+
75+
static class Pos {
76+
int i, j;
77+
78+
public Pos(int i, int j) {
79+
this.i = i;
80+
this.j = j;
81+
}
82+
}
83+
}

โ€Žsrc/Simulation/P2636/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## [baekjoon-2636] ์น˜์ฆˆ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/112263513-c610f700-8cb2-11eb-8d80-01e84e3278fb.png)
4+
![image](https://user-images.githubusercontent.com/22045163/112263652-ff496700-8cb2-11eb-9452-7bbb8325a011.png)
5+
6+
### ํ’€์ด ๊ณผ์ •
7+
8+
์–ด๋–ป๊ฒŒ ํ’€์ดํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ ํ•œ์ฐธ์„ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ดˆ์ ์„ ์น˜์ฆˆ์— ๋งž์ถ”๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ’€์ด๋ฒ•์ด ์ƒ๊ฐ๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ดˆ์ ์„ ๋ฐ”๊ฟ”์„œ ์ƒ๊ฐํ•ด๋ณด์ž. ์ด ๋ฌธ์ œ๋Š” `๋ญ‰์น˜๋ฅผ ๋‘˜๋Ÿฌ์‹ธ๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค`์— ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์น˜์ฆˆ๊ฐ€ ์•„๋‹Œ `๊ณต๊ธฐ`์— ์ดˆ์ ์„ ๋งž์ถฐ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋‚˜๊ฐ€์•ผ ํ•œ๋‹ค. ๊ณต๊ธฐ์˜ ์ž…์žฅ์—์„œ BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€์–ด๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
9+
10+
![image](https://user-images.githubusercontent.com/22045163/112263564-dcb74e00-8cb2-11eb-8b67-b125390f4bc0.png)

โ€Žsrc/Simulation/P2636/input.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
13 12
2+
0 0 0 0 0 0 0 0 0 0 0 0
3+
0 0 0 0 0 0 0 0 0 0 0 0
4+
0 0 0 0 0 0 0 1 1 0 0 0
5+
0 1 1 1 0 0 0 1 1 0 0 0
6+
0 1 1 1 1 1 1 0 0 0 0 0
7+
0 1 1 1 1 1 0 1 1 0 0 0
8+
0 1 1 1 1 0 0 1 1 0 0 0
9+
0 0 1 1 0 0 0 1 1 0 0 0
10+
0 0 1 1 1 1 1 1 1 0 0 0
11+
0 0 1 1 1 1 1 1 1 0 0 0
12+
0 0 1 1 1 1 1 1 1 0 0 0
13+
0 0 1 1 1 1 1 1 1 0 0 0
14+
0 0 0 0 0 0 0 0 0 0 0 0

0 commit comments

Comments
ย (0)