-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7569.cpp
More file actions
108 lines (100 loc) · 1.67 KB
/
7569.cpp
File metadata and controls
108 lines (100 loc) · 1.67 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 7569. 토마토
// 2019.05.21
// BFS
#include<iostream>
#include<queue>
using namespace std;
int m, n, h, cnt;
int tomato[100][100][100];
int visit[100][100][100];
// 왼쪽, 오른쪽, 앞, 뒤, 위, 아래
int dx[6] = { 1,-1,0,0,0,0 };
int dy[6] = { 0,0,1,-1,0,0 };
int dz[6] = { 0,0,0,0,1,-1 };
struct position
{
int x, y, z;
};
queue<position> q;
int BFS()
{
// 모든 토마토가 익어있는 상태일때
if (cnt == 0)
{
return 0;
}
int ans = 1;
while (!q.empty())
{
int size = q.size();
for (int i = 0; i < size; i++)
{
position pos = q.front();
q.pop();
int x = pos.x;
int y = pos.y;
int z = pos.z;
// 이미 방문 하였다면 무시
if (visit[x][y][z])
{
continue;
}
else
{
visit[x][y][z] = 1;
}
// 6가지 방향에 대해서 검사
for (int j = 0; j < 6; j++)
{
int xx = x + dx[j];
int yy = y + dy[j];
int zz = z + dz[j];
if (xx < 0 || xx >= h ||
yy < 0 || yy >= n ||
zz < 0 || zz >= m ||
tomato[xx][yy][zz] == -1)
{
continue;
}
if (tomato[xx][yy][zz] == 0)
{
q.push({ xx,yy,zz });
tomato[xx][yy][zz] = 1;
cnt--;
}
if (cnt == 0)
{
return ans;
}
}
}
ans++;
}
return -1;
}
int main()
{
cin >> m >> n >> h;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
cin >> tomato[i][j][k];
// 익지않은 토마토의 개수를 저장
if (tomato[i][j][k] == 0)
{
cnt++;
}
// 이미 익은 토마토는 BFS를 위해 큐에 삽입
else if (tomato[i][j][k] == 1)
{
q.push({ i,j,k });
}
}
}
}
cout << BFS() << endl;
return 0;
}