https://www.acmicpc.net/problem/3197
์ด์ ์ ์คํจํ๋ ๋ฌธ์ ๋ฅผ ๋ค์ ๋ณด๊ณ ์๋ค.
์ด ๋ฌธ์ ๋ BFS๋ฅผ ์ด์ฉํ ๋ฌธ์ ๋ก 1๋ ์ ๋ค์ ํ์ง ๋ชปํ๋ ๋ฌธ์ ๋ค.
๋ค์ ์ฝ๋๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ก ํ๋ฆฐ ์ฝ๋์ด๋ค.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.LinkedList;
public class B_3197 {
static int R,C,ti=0,A,B;
static int[][] dic= {{0,1},{1,0},{-1,0},{0,-1}};
static Queue<int[]> que1,que2;
static int[][] visited;
static boolean[][] ch;
static boolean check(int x,int y) {
if (x<0||x>=R) return false;
else if (y<0||y>=C) return false;
return true;
}
static boolean fedge() {//๋ฐฑ์กฐ์ฃผ๋ณ์ ๋ฌผ์ ์ฒดํฌ(์ฒดํฌ์ ๋ฐฑ์กฐ๊ฐ ๋์ค๋ฉด true๋ฅผ ๋ฐํ)
int n =que2.size();
for(int t=0;t<n;t++) {
Queue<int[]> qque = new LinkedList<>();
int[] k = que2.poll();
qque.add(k);
while(qque.isEmpty()!=true) {
int[] a = qque.poll();
int pp=visited[a[0]][a[1]];
for (int i=0;i<4;i++) {
int aa=a[0]+dic[i][0],bb=a[1]+dic[i][1];
if (check(aa,bb)==false)continue;
if (visited[aa][bb]==pp)continue;
if (visited[aa][bb]*pp==2) {
A=aa; B=bb;
return true;
}
if(visited[aa][bb]==-1) {
visited[aa][bb]=pp;
int[] w = {aa,bb};
qque.add(w);
}
else if(visited[aa][bb]==-3){
int[] w = {aa,bb};
que2.add(w);
visited[aa][bb]=pp;
}
}
}
}
return false;
}
static boolean ffedge() {
int n=que1.size();
for (int t=0;t<n;t++) {
Queue<int[]> qque = new LinkedList<>();
int[] k = que1.poll();
if (visited[k[0]][k[1]]>0) continue;
qque.add(k);
while(qque.isEmpty()!=true) {
int[] a = qque.poll();
ch[a[0]][a[1]]=true;
visited[a[0]][a[1]]=-1;
for (int i=0;i<4;i++) {
int aa=a[0]+dic[i][0],bb=a[1]+dic[i][1];
if (check(aa,bb)==false)continue;
if (ch[aa][bb]==true)continue;
if(visited[aa][bb]==-3) {
int[] w = {aa,bb};
que1.add(w);
}
else if (visited[aa][bb]==-1) {
int[] w = {aa,bb};
qque.add(w);
}
}
}
}
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
que1 = new LinkedList<>();que2 = new LinkedList<>();
visited = new int[R][C];
ch= new boolean[R][C];
int p=1;
for (int i=0;i<R;i++) {
String s =br.readLine();
for (int j=0;j<C;j++) {
char a = s.charAt(j);
int[] w = {i,j};
if (a=='.') {
visited[i][j]=-1;
que1.add(w);
}
else if (a=='X') {
visited[i][j]=-3;
}
else if (a=='L') {
que2.add(w);
visited[i][j]=p;
p+=1;
}
}
}
while(true) {
if (fedge()==true) {
if (visited[A][B]==1) ti+=1;
break;
}
ffedge();
ti+=1;
}
System.out.println(ti);
}
}
|
cs |
- ๋งค์ผ ๋ฌผ๊ณผ ์ ์ดํ ๊ฐ๋ก์ธ๋ก์ ์ผ์์ด ๋ น๋๋ค.
- ๋ฐฑ์กฐ๋ ๋ฌผ๊ณต๊ฐ์์ ์ธ๋ก๋ ๊ฐ๋ก๋ก ์์ง์ผ ์ ์๋ค.
- ๋ฉฐ์น ์ด ์ง๋์ผ ๋ฐฑ์กฐ๋ฅผ ๋ง๋ ์ ์๋์ง๋ฅผ ๊ตฌํ๋ค.
- BFS๋ฅผ ์ด์ฉํด ๋ค๋ฅธ ๋ฐฑ์กฐ๋ฅผ ๋ง๋ ์ ์๋์ง ํ์ํ๋ค.
- ๊ทผ์ฒ ์ผ์์ ๋ น์ด๋ ๋ฌผ์ ์์น๋ฅผ ๋ฐ๋ก waterQueue ์ ๋ด๋๋ค.
- ๋ค๋ฅธ ๋ฐฑ์กฐ์ ํ์์ swanQueue์์ ์งํํ๋ค. (ํ๋์ ๋ฐฑ์กฐ์์ ๋ค๋ฅธ ๋ฐฑ์กฐ๊น์ง ๊ฐ๊ธฐ ์ํด)
- visited๋ 1์ด ์์ํ๋ ๋ฐฑ์กฐ์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ์ด๊ณ , 2๊ฐ ๋๋จธ์ง ํ๋์ ๋ฐฑ์กฐ(๋ง๋์ผ ํ๋ ๋ฐฑ์กฐ)์ด๋ฉฐ, -1์ ๋ฌผ, -3์ ์ผ์์ ๋ํ๋๋ค.
- meetSwan() ์์๋ ๋ฐฑ์กฐ์ ๋ํ BFS๋ก ํ์ฌ ๋ค๋ฅธ ๋ฐฑ์กฐ์ ๋๋ฌํ ์ ์๋์ง๋ฅผ ํ์ธํ๋ฉฐ, ๋ฐฑ์กฐ์ ๊ทผ์ฒ ์ผ์์ ๊ฒฝ์ฐ ์ผ์์ด ๋ น์ ๋ค์๋ ์๋ ํ์์ด ๊ฐ๋ฅํ๊ธฐ์ ์ด๋ค์ ์ ๋ถ nextSwanQueue์ ๋ฃ์ด๋๊ณ SwanQueue๋ฅผ ์ ๋ฐ์ดํธํ๋ค.
- meltIce() ์์๋ ์ ์ฅ๋ ๋ฌผ ๊ทผ์ฒ์ ์ผ์๋ค์ ๋ น์ด๋๋ฐ ๋ น์ ์ผ์๋ค์ ์์น๋ฅผ nextWaterQueue์ ๋ฃ์ด WaterQueue๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค.
- ์๋๋ ์ฑ๊ณตํ ์ฝ๋์ด๋ค. (๋ฉ๋ชจ๋ฆฌ๋ ์๊ฐ๋ ๋ถ์กฑํ๊ธฐ์ ๋ค๋ฅธ ๋ถ๋ค์ ํ์ด๋ฅผ ์ฐธ๊ณ ํ๊ณ ๋ณด์ํด์ผํ๋ค.)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.LinkedList;
class XY {
int x, y;
public XY(int x, int y) {
this.x = x;
this.y = y;
}
}
public class B_3197 {
static int R, C;
static int[][] dic = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
static Queue<XY> swanQueue, waterQueue;
static int[][] visited;
static boolean[][] waterVisited;
static boolean check(int x, int y) {
return x >= 0 && x < R && y >= 0 && y < C;
}
static boolean meetSwan() {
Queue<XY> nextSwanQueue = new LinkedList<>();
while (!swanQueue.isEmpty()) {
XY current = swanQueue.poll();
for (int i = 0; i < 4; i++) {
int nx = current.x + dic[i][0];
int ny = current.y + dic[i][1];
if (!check(nx, ny)) continue;
if (visited[nx][ny] == 1) continue; // ์ด๋ฏธ ํ์ํ ์์ญ
if (visited[nx][ny] == 2) return true; // ๋ค๋ฅธ ๋ฐฑ์กฐ์ ๋ง๋จ
if (visited[nx][ny] == -3) { // ์ผ์์ธ ๊ฒฝ์ฐ
nextSwanQueue.add(new XY(nx, ny));
} else {
swanQueue.add(new XY(nx, ny)); // ๋ฌผ ์์ญ
}
visited[nx][ny] = 1; // ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
}
}
swanQueue = nextSwanQueue; // ์ผ์์ด ๋
น์ ๋ค์ ํ์ ๋์์ด ๋๋ ์์ญ์ ์
๋ฐ์ดํธ
return false;
}
static void meltIce() {
Queue<XY> nextWaterQueue = new LinkedList<>();
while (!waterQueue.isEmpty()) {
XY current = waterQueue.poll();
for (int i = 0; i < 4; i++) {
int nx = current.x + dic[i][0];
int ny = current.y + dic[i][1];
if (!check(nx, ny)) continue;
if (waterVisited[nx][ny]) continue; // ์ด๋ฏธ ๋
น์ ๋ฌผ
if (visited[nx][ny] == -3) { // ์ผ์์ธ ๊ฒฝ์ฐ
visited[nx][ny] = -1; // ๋ฌผ๋ก ๋ณ๊ฒฝ
nextWaterQueue.add(new XY(nx, ny)); // ๋ค์ ๋
น์ผ ์ผ์
}
}
}
waterQueue = nextWaterQueue; // ๋ค์์ ๋
น์ผ ์ผ์๋ค์ ์ ์ฅ
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
swanQueue = new LinkedList<>();
waterQueue = new LinkedList<>();
visited = new int[R][C];
waterVisited = new boolean[R][C];
XY swan1 = null, swan2 = null;
for (int i = 0; i < R; i++) {
String line = br.readLine();
for (int j = 0; j < C; j++) {
char c = line.charAt(j);
if (c == '.') {
visited[i][j] = -1; // ๋ฌผ
waterQueue.add(new XY(i, j));
waterVisited[i][j] = true;
} else if (c == 'X') {
visited[i][j] = -3; // ์ผ์
} else if (c == 'L') {
visited[i][j] = -1; // ๋ฐฑ์กฐ๊ฐ ์๋ ๋ฌผ
waterQueue.add(new XY(i, j));
waterVisited[i][j] = true;
if (swan1 == null) {
swan1 = new XY(i, j);
swanQueue.add(swan1);
visited[i][j] = 1;
} else {
swan2 = new XY(i, j);
visited[i][j] = 2;
}
}
}
}
int days = 0;
while (true) {
if (meetSwan()) {
System.out.println(days);
break;
}
meltIce();
days++;
}
}
}
|
cs |
'Coding > ๋ฐฑ์ค ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| (์๋ฐ) ๋ฐฑ์ค 7420๋ฒ : ๋งน๋ ๋ฐฉ๋ฒฝ (0) | 2026.01.20 |
|---|---|
| (ํ์ด์ฌ) ๋ฐฑ์ค 4991๋ฒ : ๋ก๋ด ์ฒญ์๊ธฐ (0) | 2026.01.09 |
| (JAVA) ๋ฐฑ์ค 3085๋ฒ : ์ฌํ ๊ฒ์ (0) | 2024.03.12 |
| (JAVA) ๋ฐฑ์ค 10026๋ฒ : ์ ๋ก์์ฝ (0) | 2023.04.03 |
| (ํ์ด์ฌ) ๋ฐฑ์ค 2839๋ฒ : ์คํ ๋ฐฐ๋ฌ (0) | 2022.07.21 |
Comment