-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTomato.cpp
More file actions
71 lines (64 loc) · 1.23 KB
/
Copy pathTomato.cpp
File metadata and controls
71 lines (64 loc) · 1.23 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
// Baekjoon(7576)
// Queue, BFS
#include <iostream>
#include <queue>
using namespace std;
typedef struct node_t {
int date;
int x_pos;
int y_pos;
}Node;
int M, N, answer, X, Y, D, X_save, Y_save;
int tomato[1001][1001];
int check_que[1001][1001];
int X_cal[4] = { -1, 1, 0, 0 };
int Y_cal[4] = { 0, 0, -1, 1 };
bool not_only_zero = false;
queue<Node>que;
void bfs() {
while (!que.empty()) {
D = que.front().date;
Y = que.front().y_pos;
X = que.front().x_pos;
que.pop();
for (int i{ 0 }; i < 4; i++) {
X_save = X + X_cal[i];
Y_save = Y + Y_cal[i];
if (X_save > 0 && X_save <= N && Y_save > 0 && Y_save <= M) {
if (tomato[X_save][Y_save] == 0) {
tomato[X_save][Y_save] = 1;
que.push(Node{ D + 1, X_save, Y_save });
}
}
}
answer = D;
}
}
int main() {
cin >> M >> N;
for (int i{ 1 }; i <= N; i++) {
for (int j{ 1 }; j <= M; j++) {
cin >> tomato[i][j];
if (tomato[i][j] == 1) {
que.push(Node{ 0,i,j });
not_only_zero = true;
}
}
}
if (not_only_zero == false) {
cout << 0 << endl;
return 0;
}
else {
bfs();
}
for (int i{ 1 }; i <= N; i++) {
for (int j{ 1 }; j <= M; j++) {
if (tomato[i][j] == 0) {
cout << -1 << endl;
return 0;
}
}
}
cout << answer << endl;
}