# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
20218 | kuni0606 | 토마토 (3차원) (KOI13_tomato3D) | C++98 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
using namespace std;
int m, n, h;
int arr[1000001][101] = { 0, };
queue < pair<pair<int, int>, int>> q;
int main(){
cin >> m >> n >> h;
int a = 0;//모두 익은 경우
int b = 0;//모두 익을 수 없는 경우
int dir[6][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { n, 0 }, { -n, 0 } };
for (int i = 1; i <= h*n; i++){
for (int j = 1; j <= m; j++){
cin >> arr[i][j];
if (arr[i][j] == 0){
a++;
}
else if (arr[i][j] == 1){
q.push(make_pair(make_pair(i, j), 0));
}
}
}
int step = 0;
//모두 익어 있는 경우 0출력
if (a == 0){
cout << 0 << endl;
}
else{
while (!q.empty()){
while (step == q.front().second){
for (int z = 0; z < 6; z++){
int s = q.front().first.first + dir[z][0];
int e = q.front().first.second + dir[z][1];
if (s<1 || s>n*h || e<1 || e>m)
continue;
if (z == 0 || z == 1 || z == 2 || z == 3){
if (s < (q.front().first.first / n)*n + 1 || s > (q.front().first.first / n)*n + n)
continue;
}
if (arr[s][e] == -1 || arr[s][e] == 1)
continue;
arr[s][e] = 1;
q.push(make_pair(make_pair(s, e), step+1));
}
q.pop();
if (q.empty()==true){
break;
}
}
step++;
}
for (int i = 1; i <= h*n; i++){
for (int j = 1; j <= m; j++){
if (arr[i][j] == 0){
b++;
}
}
}
if (b != 0){
cout << -1 << endl;
}
else{
cout << step-1 << endl;
}
}
return 0;
}