-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.cpp
More file actions
77 lines (69 loc) · 1.44 KB
/
7576.cpp
File metadata and controls
77 lines (69 loc) · 1.44 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
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
int dx[] = { -1,0,1,0};
int dy[] = { 0,1,0,-1 };
int visited[1001][1001];
int graph[1001][1001];
int main(void) {
int n, m;
scanf("%d %d", &n, &m);
queue<pair<int, int>>q;
for (unsigned short i = 0; i < m; ++i)
{
for (unsigned short j = 0; j < n; ++j)
{
scanf("%d", &graph[i][j]);
if (graph[i][j] == 1)
{
q.push(make_pair(i, j));
visited[i][j] = graph[i][j];
}
if (graph[i][j] == -1)
{
visited[i][j] = graph[i][j];
}
}
}
while (!q.empty())
{
int x, y;
x = q.front().first, y = q.front().second;
q.pop();
for (unsigned int i = 0; i < 4; ++i)
{
int xn = x + dx[i], yn = y + dy[i];
//밖으로 나가는 경우없이, 안익은 토마토만을 찾고 실제로 visited에도
//안익은 토마토로 검증되어있는 토마토들만
if (xn >= 0 && xn < m && yn >= 0 && yn < n &&
graph[xn][yn] == 0 && graph[xn][yn] != -1 && visited[xn][yn] == 0)
{
q.push(make_pair(xn, yn));
visited[xn][yn] = visited[x][y] + 1;
//단순 while문을 이용해서 q에 사분위를 넣어준다음 검색한다.
}
}
}
int tdays = 0;
for (unsigned short i = 0; i < m; ++i)
{
for (int unsigned short j = 0; j < n; ++j)
{
if (tdays < visited[i][j])
tdays = visited[i][j];
}
}
for (unsigned short i = 0; i <m ; ++i)
{
for (unsigned short j = 0; j <n ; ++j)
{
if (visited[i][j] == 0)
{
tdays = 0;
}
}
}
printf("%d\n", tdays - 1);
return 0;
}