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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
| #include <iostream>
#include <vector>
#include <list>
#define MAX_WIDTH 1500
using namespace std;
typedef struct Location {
int row;
int col;
} Location;
int row, col, todayLength;
Location bird1, bird2;
Location meltToday[MAX_WIDTH*MAX_WIDTH];
char map[MAX_WIDTH][MAX_WIDTH];
bool visit[MAX_WIDTH][MAX_WIDTH], meltVisit[MAX_WIDTH][MAX_WIDTH];
list<Location> q;
bool init();
void initBird(Location bird, char c);
bool meltIce();
bool checkBird(int r, int c);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> row >> col;
bool check = false;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> map[i][j];
if (map[i][j] == 'L') {
if (!check) {
bird1.row = i;
bird1.col = j;
check = true;
}
else {
bird2.row = i;
bird2.col = j;
map[i][j] = 'l';
}
}
}
}
if (init()) {
cout << 0;
return 0;
}
int day = 1;
while (true) {
cout << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
cout << map[i][j];
cout << endl;
}
cout << endl;
if (meltIce()) {
cout << day;
break;
}
day++;
}
}
bool init() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (map[i][j] == 'X') {
if (i > 0 && map[i - 1][j] != 'X') {
meltToday[todayLength++] = { i, j };
meltVisit[i][j] = true;
}
else if (j < col - 1 && map[i][j + 1] != 'X') {
meltToday[todayLength++] = { i, j };
meltVisit[i][j] = true;
}
else if (i < row - 1 && map[i + 1][j] != 'X') {
meltToday[todayLength++] = { i, j };
meltVisit[i][j] = true;
}
else if (j > 0 && map[i][j - 1] != 'X') {
meltToday[todayLength++] = { i, j };
meltVisit[i][j] = true;
}
}
}
}
initBird(bird1, 'L');
if (map[bird2.row][bird2.col] == 'L')
return true;
initBird(bird2, 'l');
return false;
}
void initBird(Location bird, char c) {
q.push_back({ bird.row, bird.col });
visit[bird.row][bird.col] = true;
while (!q.empty()) {
Location temp = q.front();
q.pop_front();
map[temp.row][temp.col] = c;
if (temp.row > 0 && !visit[temp.row - 1][temp.col] && map[temp.row - 1][temp.col] != 'X') {
q.push_back({ temp.row - 1, temp.col });
visit[temp.row - 1][temp.col] = true;
}
if (temp.row < row - 1 && !visit[temp.row + 1][temp.col] && map[temp.row + 1][temp.col] != 'X') {
q.push_back({ temp.row + 1, temp.col });
visit[temp.row + 1][temp.col] = true;
}
if (temp.col > 0 && !visit[temp.row][temp.col - 1] && map[temp.row][temp.col - 1] != 'X') {
q.push_back({ temp.row, temp.col -1 });
visit[temp.row][temp.col - 1] = true;
}
if (temp.col < col - 1 && !visit[temp.row][temp.col + 1] && map[temp.row][temp.col + 1] != 'X') {
q.push_back({ temp.row, temp.col + 1});
visit[temp.row][temp.col + 1] = true;
}
}
}
bool meltIce() {
int index = 0;
vector<Location> q;
while (index < todayLength) {
if (checkBird(meltToday[index].row, meltToday[index].col))
return true;
if (meltToday[index].row > 0 && map[meltToday[index].row - 1][meltToday[index].col] == 'X' && !meltVisit[meltToday[index].row-1][meltToday[index].col] ) {
q.push_back({ meltToday[index].row - 1, meltToday[index].col });
meltVisit[meltToday[index].row - 1][meltToday[index].col] = true;
}
if (meltToday[index].col < col - 1 && map[meltToday[index].row][meltToday[index].col + 1] == 'X' && !meltVisit[meltToday[index].row][meltToday[index].col + 1]) {
q.push_back({ meltToday[index].row, meltToday[index].col + 1 });
meltVisit[meltToday[index].row][meltToday[index].col + 1] = true;
}
if (meltToday[index].row < row - 1 && map[meltToday[index].row + 1][meltToday[index].col] == 'X' && !meltVisit[meltToday[index].row + 1][meltToday[index].col]) {
q.push_back({ meltToday[index].row + 1, meltToday[index].col });
meltVisit[meltToday[index].row + 1][meltToday[index].col] = true;
}
if (meltToday[index].col > 0 && map[meltToday[index].row][meltToday[index].col - 1] == 'X' && !meltVisit[meltToday[index].row][meltToday[index].col - 1]) {
q.push_back({ meltToday[index].row, meltToday[index].col - 1 });
meltVisit[meltToday[index].row][meltToday[index].col - 1] = true;
}
index++;
}
todayLength = 0;
//넣기
for (int i = 0; i < q.size(); i++)
meltToday[todayLength++] = q[i];
return false;
}
bool checkBird(int r, int c) {
bool checkL = false, checkl = false;
if (r > 0 && map[r - 1][c] == 'L')
checkL = true;
else if (r < row - 1 && map[r + 1][c] == 'L')
checkL = true;
else if (c > 0 && map[r][c - 1] == 'L')
checkL = true;
else if (c < col - 1 && map[r][c + 1] == 'L')
checkL = true;
if (r > 0 && map[r - 1][c] == 'l')
checkl = true;
else if (r < row - 1 && map[r + 1][c] == 'l')
checkl = true;
else if (c > 0 && map[r][c - 1] == 'l')
checkl = true;
else if (c < col - 1 && map[r][c + 1] == 'l')
checkl = true;
if (checkL && checkl)
return true;
else if (checkL) {
map[r][c] = 'L';
initBird({ r,c }, 'L');
if (map[bird2.row][bird2.col] == 'L')
return true;
}
else if (checkl) {
map[r][c] = 'l';
initBird({ r,c }, 'l');
if (map[bird1.row][bird1.col] == 'l')
return true;
}
else
map[r][c] = '.';
return false;
}
|