-
[7569] 토마토BOJ 2021. 10. 16. 09:51
https://www.acmicpc.net/problem/7569
7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
<문제>
2차원에서 방향데이터 2개, 각각 4방향이였다면
3차원에서는 방향데이터 3개, 각각 6방향으로 구현하면 된다.
3차원인것을 제외하면 크게 달라진 점은 없다.
인덱스 헷갈려서 실수하기 쉬우므로, 순서에 유의하여 구현해야 하겠다
<소스코드>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970#include <limits.h>#include <stdio.h>#include <algorithm>#include <queue>using namespace std;queue<int> q;int n, m, height, a[101][101][101], dp[101][101][101];int dy[6] = {0, 1, 0, -1, 0, 0}, dx[6] = {1, 0, -1, 0, 0, 0},dh[6] = {0, 0, 0, 0, 1, -1};void f(void) {int ty, tx, th, y, x, h, i;while (!q.empty()) {th = q.front();q.pop();ty = q.front();q.pop();tx = q.front();q.pop();for (i = 0; i < 6; i++) {h = th + dh[i];y = ty + dy[i];x = tx + dx[i];if (y < 0 || x < 0 || h < 0 || y >= n || x >= m || h >= height)continue;if (dp[h][y][x] >= 0) continue;if (a[h][y][x] == 0) {q.push(h);q.push(y);q.push(x);dp[h][y][x] = dp[th][ty][tx] + 1;a[h][y][x] = 1;}}}return;}int main(void) {int i, j, k;scanf("%d %d %d", &m, &n, &height);for (i = 0; i < height; i++) {for (j = 0; j < n; j++) {for (k = 0; k < m; k++) {scanf("%d", &a[i][j][k]);if (a[i][j][k] == 1) {q.push(i);q.push(j);q.push(k);}if (a[i][j][k] == 0) dp[i][j][k] = -1;}}}int ans = 0;f();for (i = 0; i < height; i++) {for (j = 0; j < n; j++) {for (k = 0; k < m; k++) {if (dp[i][j][k] == -1) {ans = -1;goto allBreak;}ans = max(ans, dp[i][j][k]);}}}allBreak:printf("%d", ans);return 0;}cs 'BOJ' 카테고리의 다른 글
[2178] 미로 탐색 (0) 2021.10.16 [4963] 섬의 개수 (0) 2021.10.16 [2667] 단지번호붙이기 (0) 2021.10.16 [16946] 벽 부수고 이동하기 4 (0) 2021.10.16 [15903] 카드 합체 놀이 (0) 2021.10.14