๐ŸŽฒ ๋ฐฑ์ค€ 7569๋ฒˆ ํ† ๋งˆํ† 

Jeongeunยท2023๋…„ 8์›” 2์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
105/187

๋ฐฑ์ค€ 7569๋ฒˆ

๐Ÿงธ 3์ฐจ์›๋ฐฐ์—ด์ด๋ผ ๋งŽ์ด ํ—ท๊ฐˆ๋ ธ๋‹ค.. ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ๋„ ๊ณ ์ƒํ–ˆ๋‹ค..
๐Ÿ’Š ์ฒ˜์Œ์— ์ต์€ ํ† ๋งˆํ† ๋“ค์ด ํ•œ๋ฒˆ์— ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด์•ผํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒํ• ๊นŒ ๊ณ ๋ฏผ์„ ํ•˜๋‹ค ๊ฒฐ๊ตญ ๋‹ต์„ ํ™•์ธํ•ด๋ดค๋‹ค.
๐Ÿ’ก ์ •๋ง ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค. ์ฒ˜์Œ์— ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ์‹œ์ž‘ํ•˜๊ธฐ..! ์™œ ์ด๊ฑธ ์ƒ๊ฐ์„ ๋ชปํ–ˆ์„๊นŒ๐Ÿ˜ข
๐Ÿ’Š ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.
๐Ÿ’ก queue๋ฅผ ์ฒ˜๋ฆฌํ• ๋•Œ shift()๊ฐ€ ์•„๋‹Œ index๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค์ฃผ๋ฉด์„œ ํ•ด์•ผํ–ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฌ๋‚˜๋ฅผ ๊นจ๋‹ฌ์•˜๋‹ค...

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n")
  .map((item) => item.split(" ").map(Number));
const [M, N, H] = input.shift();
const boxes = Array.from(new Array(H), () => []);

const queue = [];

for (let h = 0; h < H; h++) {
  for (let n = 0; n < N; n++) {
    boxes[h].push(input[n + h * N]);
    input[n + h * N].forEach((num, index) => {
      if (num === 1) {
        queue.push([h, n, index, 1]);
      }
    });
  }
}

const dir = [
  [0, 0, 1],
  [0, 0, -1],
  [0, 1, 0],
  [0, -1, 0],
  [1, 0, 0],
  [-1, 0, 0],
];

let index = 0;
const BFS = () => {
  while (index <= queue.length - 1) {
    const [ph, pn, pm, day] = queue[index];
      index++;
      
    for (let d = 0; d < 6; d++) {
      const nh = ph + dir[d][0];
      const nn = pn + dir[d][1];
      const nm = pm + dir[d][2];

      if (
        nh >= 0 &&
        nn >= 0 &&
        nm >= 0 &&
        nh < H &&
        nn < N &&
        nm < M &&
        boxes[nh][nn][nm] === 0
      ) {
        queue.push([nh, nn, nm, day + 1]);
        boxes[nh][nn][nm] = day + 1;
      }
    }
  }
};

BFS();

let result = 0;

for (let h = 0; h < H; h++) {
  for (let n = 0; n < N; n++) {
    for (let m = 0; m < M; m++) {
      if (boxes[h][n][m] === 0) {
        return console.log(-1);
      } else {
        if (result < boxes[h][n][m]) {
          result = boxes[h][n][m];
        }
      }
    }
  }
}

console.log(result - 1);

0๊ฐœ์˜ ๋Œ“๊ธ€