Submission #720853

# Submission time Handle Problem Language Result Execution time Memory
720853 2023-04-09T14:32:58 Z psyms5050 토마토 (3차원) (KOI13_tomato3D) C++14
20 / 20
207 ms 13172 KB
#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

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
1 Correct 2 ms 1236 KB Output is correct
2 Correct 6 ms 1344 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 704 KB Output is correct
2 Correct 6 ms 2628 KB Output is correct
3 Correct 4 ms 1876 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 980 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2388 KB Output is correct
2 Correct 29 ms 2676 KB Output is correct
3 Correct 33 ms 3392 KB Output is correct
4 Correct 60 ms 5180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1364 KB Output is correct
2 Correct 5 ms 968 KB Output is correct
3 Correct 31 ms 3456 KB Output is correct
4 Correct 9 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 4212 KB Output is correct
2 Correct 28 ms 3504 KB Output is correct
3 Correct 59 ms 4632 KB Output is correct
4 Correct 13 ms 2248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 8772 KB Output is correct
2 Correct 64 ms 5668 KB Output is correct
3 Correct 129 ms 9400 KB Output is correct
4 Correct 20 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 8392 KB Output is correct
2 Correct 118 ms 9916 KB Output is correct
3 Correct 109 ms 7656 KB Output is correct
4 Correct 131 ms 10820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 11872 KB Output is correct
2 Correct 144 ms 10748 KB Output is correct
3 Correct 162 ms 11376 KB Output is correct
4 Correct 173 ms 12300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 12360 KB Output is correct
2 Correct 87 ms 6060 KB Output is correct
3 Correct 207 ms 13172 KB Output is correct
4 Correct 71 ms 5144 KB Output is correct