-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7576.cpp
97 lines (91 loc) · 1.44 KB
/
7576.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 7576. 토마토
// 2019.05.21
// BFS
#include<iostream>
#include<queue>
using namespace std;
int map[1000][1000];
int visit[1000][1000];
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
int n, m;
queue<pair<int, int>> q;
int cnt;
int BFS()
{
int ans = 1;
int x, y;
while (!q.empty())
{
int size = q.size();
for (int i = 0; i < size; ++i)
{
pair<int, int> temp = q.front();
q.pop();
x = temp.first;
y = temp.second;
//이미 방문했다면 다음으로넘어감
if (visit[x][y])
{
continue;
}
else
{
visit[x][y] = 1;
}
for (int j = 0; j < 4; ++j)
{
int xx = x + dx[j];
int yy = y + dy[j];
if (xx < 0 || xx >= n || yy < 0 || yy >= m || map[xx][yy] == -1)
{
continue;
}
//익지않은 토마토라면 그 위치를 큐에 넣고 익게함
else if (map[xx][yy] == 0)
{
q.push({ xx,yy });
map[xx][yy] = 1;
cnt--;
}
// 익지않은 토마토가 없을 때
if (cnt == 0)
{
return ans;
}
}
}
ans++;
}
return -1;
}
int main()
{
cin >> m >> n;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> map[i][j];
// 익은 토마토들은 미리 넣어둠
if (map[i][j] == 1)
{
q.push({ i,j });
}
// 익지않은 토마토의 갯수를 셈.
else if (map[i][j] == 0)
{
cnt++;
}
}
}
if (cnt == 0)
{
cout << 0 << endl;
}
else
{
cout << BFS() << endl;
}
return 0;
}