7576. 토마토 - node.js / javascript

윤상준·2022년 5월 5일
0

BOJ - node.js / javascript

목록 보기
39/55
post-thumbnail

문제

내 코드

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");

let [M, N] = input[0].split(" ").map(Number);
let box = input.slice(1).map((v) => v.split(" ").map(Number));
let ch = Array.from({ length: 1001 }, () => Array(1001).fill(0));
const dx = [1, 0, -1, 0];
const dy = [0, 1, 0, -1];

const queue = [];
let answer = 0;

for (let i = 0; i < N; i++) {
  for (let j = 0; j < M; j++) {
    if (box[i][j] === 1) {
      queue.push([i, j, 0]);
    }
  }
}

let idx = 0;
let day = 0;

while (idx !== queue.length) {
  let [y, x, cnt] = queue[idx];
  for (let i = 0; i < dx.length; i++) {
    const [nx, ny] = [x + dx[i], y + dy[i]];
    if (nx >= 0 && nx < M && ny >= 0 && ny < N) {
      if (box[ny][nx] === 0) {
        box[ny][nx] = box[y][x] + 1;
        queue.push([ny, nx, cnt + 1]);
      }
    }
  }
  day = cnt;
  idx++;
}

answer = day;

for (let i = 0; i < N; i++) {
  for (let j = 0; j < M; j++) {
    if (box[i][j] === 0) {
      answer = -1;
      break;
    }
  }
  if (answer === -1) break;
}

console.log(answer);

깃허브 링크

https://github.com/highjoon/Algorithm/blob/master/BOJ/7576.js

profile
하고싶은건 많은데 시간이 없다!

0개의 댓글