

5 3 1
0 -1 0 0 0
-1 -1 0 1 1
0 0 0 1 1
-1
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
4
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
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";
}
