-
Notifications
You must be signed in to change notification settings - Fork 0
/
10102.cpp
66 lines (59 loc) · 2.06 KB
/
10102.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
58
59
60
61
62
63
64
65
66
#include <algorithm>
#include <array>
#include <iostream>
#include <queue>
#include <string>
struct Tile {
int x, y, l;
};
int main(void)
{
int size, max_step;
Tile curr;
std::queue<Tile> q;
std::array<std::string, 1000> board;
std::array<std::array<bool, 1000>, 1000> seen;
while (std::cin >> size) {
for (int i = 0; i < size; ++i) {
std::cin >> board[i];
seen[i].fill(false);
for (int j = 0; j < size; ++j) {
if (board[i][j] == '3') {
q.push({i, j, 0});
seen[i][j] = true;
}
}
}
max_step = 0;
while (!q.empty()) {
curr = q.front();
q.pop();
if (curr.x - 1 > -1 && !seen[curr.x - 1][curr.y]) {
seen[curr.x - 1][curr.y] = true;
if (board[curr.x - 1][curr.y] == '1')
max_step = std::max(max_step, curr.l + 1);
q.push({curr.x - 1, curr.y, curr.l + 1});
}
if (curr.x + 1 < size && !seen[curr.x + 1][curr.y]) {
seen[curr.x + 1][curr.y] = true;
if (board[curr.x + 1][curr.y] == '1')
max_step = std::max(max_step, curr.l + 1);
q.push({curr.x + 1, curr.y, curr.l + 1});
}
if (curr.y - 1 > -1 && !seen[curr.x][curr.y - 1]) {
seen[curr.x][curr.y - 1] = true;
if (board[curr.x][curr.y - 1] == '1')
max_step = std::max(max_step, curr.l + 1);
q.push({curr.x, curr.y - 1, curr.l + 1});
}
if (curr.y + 1 < size && !seen[curr.x][curr.y + 1]) {
seen[curr.x][curr.y + 1] = true;
if (board[curr.x][curr.y + 1] == '1')
max_step = std::max(max_step, curr.l + 1);
q.push({curr.x, curr.y + 1, curr.l + 1});
}
}
std::cout << max_step << std::endl;
}
return 0;
}