알고리즘/백준
백준 7569 [nodejs] 토마토 3차원버전
민트초코수장
2021. 2. 4. 09:30
const line = require("fs").readFileSync("/dev/stdin", "utf8");
let input = line.trim().split("\n");
//3차원이므로 dz부분을 추가하고 위,아래,동,서,남,북 6가지로 나타내었다.
const dx = [1, -1, 0, 0, 0, 0];
const dy = [0, 0, 1, -1, 0, 0];
const dz = [0, 0, 0, 0, 1, -1];
const [m, n, h] = input[0].split(" ").map(Number);
input.splice(0, 1);
let totalBox = [];
let tomatos = [];
let queue = [];
let zero = 0;
let cnt = 0;
minimumDate();
function minimumDate() {
for (let box = 0; box < h; box++) {
for (let i = 0; i < n; i++) {
tomatos.push(input[i].split(" ").map(Number));
for (let j = 0; j < m; j++) {
if (tomatos[i][j] === 1) queue.push([box, i, j]);
if (tomatos[i][j] === 0) zero++;
}
}
input.splice(0, n);
totalBox.push(tomatos);
tomatos = [];
}
bfs(queue);
zero !== 0 ? console.log(-1) : console.log(cnt - 2);
}
function bfs(queue) {
let queueCursor = 0;
while (queue.length > queueCursor) {
const [x, y, z] = queue[queueCursor++];
cnt = totalBox[x][y][z] + 1;
for (let i in dx) {
let nx = x + dx[i];
let ny = y + dy[i];
let nz = z + dz[i];
if (check(nx, ny, nz) && totalBox[nx][ny][nz] === 0) {
zero--;
queue.push([nx, ny, nz]);
totalBox[nx][ny][nz] = cnt;
}
}
}
}
function check(x, y, z) {
if (x > -1 && z > -1 && y > -1 && x < h && z < m && y < n) return true;
return false;
}
토마토 문제에서 dz만 추가된 로직이기 때문에 푸는 것에는 문제가 없었다.
다만 처음 토마토 밭을 만들때 구성을 맞추기가 살짝 어려웠다.
반응형