반응형
https://www.acmicpc.net/problem/7569
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
|
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int M, N, H;
int tomato[100][100][100];
int MAX_value;
int current_value;
int dx[6] = { 0,0,1,0,-1,0 };
int dy[6] = { 0,0,0,-1,0,1 };
int dz[6] = { 1,-1,0 ,0,0, 0 };
struct TOMATO
{
int y;
int x;
int z;
};
int result=1;
void BFS(int y, int x, int z,int current, queue<TOMATO>& q) {
if (y >= N || x >= M || z >= H || y < 0 || x < 0 || z < 0) return;
if (tomato[y][x][z] == 0){
tomato[y][x][z] = current;
q.push({ y,x,z });
result = current;
current_value++;
}
}
int main(void) {
cin >> M >> N >> H;
MAX_value = M * N * H;
current_value = 0;
queue<TOMATO> q;
for (int k = 0;k < H;k++) {
for (int i = 0;i < N;i++) {
for (int j = 0;j < M;j++) {
cin >> tomato[i][j][k];
if (tomato[i][j][k] == 1) {
current_value++;
q.push({ i,j,k });
}
else if (tomato[i][j][k] == -1) {
MAX_value--;
}
}
}
}
while (!q.empty()) {
int x, y, z;
x = q.front().x;
y = q.front().y;
z = q.front().z;
for (int i = 0;i < 6;i++) {
int xn = x + dx[i];
int yn = y + dy[i];
int zn = z + dz[i];
BFS(yn, xn, zn, tomato[y][x][z] + 1,q);
}
q.pop();
}
if (MAX_value == current_value)
cout << result-1<<endl;
else
cout << -1 << endl;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'ProgramSoliving' 카테고리의 다른 글
백준 : 2206 * (0) | 2019.12.19 |
---|---|
백준 : 1697 (0) | 2019.12.18 |
백준 : 7576 (0) | 2019.12.17 |
백준 : 1012 (0) | 2019.12.16 |
백준 : 2606 (0) | 2019.12.16 |