Repository navigation
Expand file tree
/
Copy patha62_q2a_pandemic.cpp
More file actions
58 lines (52 loc) · 1.51 KB
/
Copy patha62_q2a_pandemic.cpp
File metadata and controls
58 lines (52 loc) · 1.51 KB
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
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
int main() {
std::ios_base::sync_with_stdio(false);std::cin.tie(0);
int r, c, t;std::cin >> r >> c >> t; // 1..r, 1..c
vector<vector<int>> mtx(r+1, vector<int>(c+1, 0)); // number and visited
int res = 0;
queue<pair<int, int>> bfs; //node is pair
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
std::cin >> mtx[i][j];
if (mtx[i][j] == 1) {
++res;
bfs.push({i, j});
}
}
}
while (t--) {
vector<pair<int, int>> tmp;
while (! bfs.empty()) {
int i = bfs.front().first, j = bfs.front().second;
bfs.pop();
if (i > 1 && mtx[i-1][j] == 0) {
mtx[i-1][j] = 1;
tmp.push_back({i-1, j});
++res;
}
if (j > 1 && mtx[i][j-1] == 0) {
mtx[i][j-1] = 1;
tmp.push_back({i, j-1});
++res;
}
if (i < r && mtx[i+1][j] == 0) {
mtx[i+1][j] = 1;
tmp.push_back({i+1, j});
++res;
}
if (j < c && mtx[i][j+1] == 0) {
mtx[i][j+1] = 1;
tmp.push_back({i, j+1});
++res;
}
}
while (tmp.size() > 0) {
bfs.push(*(tmp.end()-1));
tmp.pop_back();
}
}
std::cout << res << "\n";
return 0;
}