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>
#define SIZE 110
using namespace std;
typedef struct struct_pos
{
int z;
int y;
int x;
}pos;
queue<pos> q;
int tomato[SIZE][SIZE][SIZE];
int check[SIZE][SIZE][SIZE];
int day, width, length, height;
int dfs();
bool check_fun();
int main()
{
cin >> width >> length >> height;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < length; j++)
{
for(int k = 0; k < width; k++)
{
cin >> tomato[i][j][k];
if(tomato[i][j][k] == 1)
{
q.push({i,j,k});
check[i][j][k] = 1;
}
}
}
}
day = dfs() - 1;
if(check_fun())
{
cout << day << "\n";
return 0;
}
cout << -1 << "\n";
return 0;
}
int dfs()
{
int x, y, z, temp_x, temp_y, temp_z;
int x_pos[] = {0, 1, 0, -1};
int y_pos[] = {1, 0, -1, 0};
int z_pos[] = {1, -1};
while(!q.empty())
{
z = q.front().z;
y = q.front().y;
x = q.front().x;
q.pop();
for(int i = 0; i < 4; i++)
{
temp_y = y + y_pos[i];
temp_x = x + x_pos[i];
if(temp_y < length && temp_y >= 0 && temp_x < width && temp_x >= 0)
{
if((check[z][temp_y][temp_x] == 0 || check[z][temp_y][temp_x] > check[z][y][x] + 1) && tomato[z][temp_y][temp_x] != -1)
{
q.push({z, temp_y, temp_x});
check[z][temp_y][temp_x] = check[z][y][x] + 1;
}
}
}
for(int i = 0; i < 2; i++)
{
temp_z = z + z_pos[i];
if(temp_z < height && temp_z >= 0)
{
if((check[temp_z][y][x] == 0 || check[temp_z][y][x] > check[z][y][x] + 1) && tomato[temp_z][y][x] != -1)
{
q.push({temp_z, y, x});
check[temp_z][y][x] = check[z][y][x] + 1;
}
}
}
}
return check[z][y][x];
}
bool check_fun()
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < length; j++)
{
for(int k = 0; k < width; k++)
{
if(check[i][j][k] == 0 && tomato[i][j][k] >= 0)
{
return false;
}
}
}
}
return true;
}
Compilation message (stderr)
cc.cpp: In function 'int dfs()':
cc.cpp:85:22: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
85 | return check[z][y][x];
| ^
cc.cpp:85:22: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
cc.cpp:85:22: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |