-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontan.cpp
57 lines (45 loc) · 1.04 KB
/
fontan.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int R, C;
char grid[51][51];
int dj[] = {-1, 1};
void bfs() {
queue<pii> q;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
if (grid[i][j] == 'V') q.push(make_pair(i, j));
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
if (i == R - 1) continue;
char& below = grid[i + 1][j];
if (below == 'V') continue;
if (below == '.') {
below = 'V';
q.push(make_pair(i + 1, j));
} else {
for (int d = 0; d < 2; d++) {
int j2 = j + dj[d];
if (j2 < 0 || j2 == C) continue;
char& side = grid[i][j2];
if (side == '.') {
side = 'V';
q.push(make_pair(i, j2));
}
}
}
}
}
void solve() {
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) scanf(" %c", &grid[i][j]);
bfs();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) cout << grid[i][j];
cout << endl;
}
}
int main() {
while (scanf("%d %d", &R, &C) == 2) solve();
}