-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask11a.cpp
More file actions
64 lines (59 loc) · 1.92 KB
/
task11a.cpp
File metadata and controls
64 lines (59 loc) · 1.92 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
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
vector<vector<int>> board;
string line;
while (getline(cin, line)) {
board.push_back(vector<int> (line.size()));
for (int i = 0; i < line.size(); ++i) {
board.back()[i] = line[i] - '0';
}
}
int numRows = board.size(), numCols = board[0].size();
int numFlash = 0;
int horiz[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int verti[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
for (int t = 0; t < 100; ++t) {
queue<pair<int, int>> q;
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
board[i][j]++;
if (board[i][j] > 9) {
q.push(make_pair(i, j));
}
}
}
while (!q.empty()) {
numFlash++;
pair<int, int> cur = q.front(); q.pop();
int curRow = cur.first, curCol = cur.second;
for (int k = 0; k < 8; ++k) {
int newRow = curRow + verti[k];
int newCol = curCol + horiz[k];
if (newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols) {
board[newRow][newCol]++;
if (board[newRow][newCol] == 10) {
q.push(make_pair(newRow, newCol));
}
}
}
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
if (board[i][j] > 9) {
board[i][j] = 0;
}
}
}
}
cout << numFlash;
/*for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
cout << board[i][j] << ' ';
}
cout << '\n';
}*/
return 0;
}