[C++] 7569 : 토마토

리폐·2023년 11월 14일

백준

목록 보기
16/18

📝 문제

7569 : 토마토


✏️ 입력1

5 3 1
0 -1 0 0 0
-1 -1 0 1 1
0 0 0 1 1

💻 출력1

-1

✏️ 입력2

5 3 2
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0

💻 출력2

4

✏️ 입력3

4 3 2
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
-1 -1 -1 -1
1 1 1 -1

💻 출력3

0

💭 문제해결법

7576 토마토에서 Z 축을 추가하여 푸는 문제
STL Tuple을 사용하여 3차원 표현
입력순서를 제대로 확인하지 않아서 계속해서 틀리게 되었다.


⌨️ 소스코드

7569 토마토
#include <iostream>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;

int m, n, h;
queue<tuple<int, int, int>> que;
int board[102][102][102];
int dist[102][102][102];
int dx[6] = { 0, 0, 1, -1, 0, 0 };
int dy[6] = { 1, -1, 0, 0, 0, 0 };
int dz[6] = { 0, 0, 0, 0, 1, -1 };
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> m >> n >> h; //입력순서 지키기
	for (int k = 0; k < h; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> board[i][j][k];
				if (board[i][j][k] == 1) que.push({ i, j, k });
				if (board[i][j][k] == 0) dist[i][j][k] = -1;
			}
		}
	}

	while (!que.empty()) {
		tuple<int, int, int> tue = que.front(); que.pop();
		int curX, curY, curZ;
		tie(curX, curY, curZ) = tue;
		for (int dir = 0; dir < 6; dir++) {
			int nx = curX + dx[dir];
			int ny = curY + dy[dir];
			int nz = curZ + dz[dir];
			if (nx < 0 || nx >= n || ny < 0 || ny >= m || nz < 0 || nz >= h) continue;
			if (dist[nx][ny][nz] >= 0) continue;
			dist[nx][ny][nz] = dist[curX][curY][curZ] + 1;
			que.push({ nx, ny, nz });

		}
	}

	int ans = 0;
	for (int k = 0; k < h; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (dist[i][j][k] == -1) {
					cout << -1;
					return 0;
				}
				ans = max(ans, dist[i][j][k]);
			}
		}
	}
	cout << ans << "\n";
}

profile
Unreal 5, Unity 공부

0개의 댓글