forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.cpp
More file actions
81 lines (70 loc) · 1.63 KB
/
Copy path7576.cpp
File metadata and controls
81 lines (70 loc) · 1.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int N, M;
int board[1001][1001];
bool visited[1001][1001];
int days = 1;
list<int> bfsQueueX;
list<int> bfsQueueY;
void bfs() {
if (bfsQueueX.empty()) return;
int x = bfsQueueX.front(); int y = bfsQueueY.front();
bfsQueueX.pop_front(); bfsQueueY.pop_front();
if (x > 0 && board[x - 1][y] == 0 && !visited[x - 1][y]) {
board[x - 1][y] = board[x][y] + 1;
visited[x - 1][y] = true;
bfsQueueX.push_back(x - 1); bfsQueueY.push_back(y);
}
if (y > 0 && board[x][y - 1] == 0 && !visited[x][y - 1]) {
board[x][y - 1] = board[x][y] + 1;
visited[x][y - 1] = true;
bfsQueueX.push_back(x); bfsQueueY.push_back(y - 1);
}
if (x < N - 1 && board[x + 1][y] == 0 && !visited[x + 1][y]) {
board[x + 1][y] = board[x][y] + 1;
visited[x + 1][y] = true;
bfsQueueX.push_back(x + 1); bfsQueueY.push_back(y);
}
if (y < M - 1 && board[x][y + 1] == 0 && !visited[x][y + 1]) {
board[x][y + 1] = board[x][y] + 1;
visited[x][y + 1] = true;
bfsQueueX.push_back(x); bfsQueueY.push_back(y + 1);
}
bfs();
}
int main()
{
int i, j;
cin >> M >> N;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
cin >> board[i][j];
visited[i][j] = false;
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
if (board[i][j] == 1) {
bfsQueueX.push_back(i);
bfsQueueY.push_back(j);
visited[i][j] = true;
}
}
}
bfs();
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
if (board[i][j] == 0) {
days = 0; i = N;
break;
}
if (board[i][j] != -1 && board[i][j] > days) {
days = board[i][j];
}
}
}
cout << days - 1;
return 0;
}