다윤 풀이
const filePath = process.platform === "linux" ? "/dev/stdin" : "input.txt"; let [MN, ...inputs] = require("fs") .readFileSync(filePath) .toString() .trim() .split("\n"); const [M, N] = MN.split(" ").map((a) => +a); const board = inputs.map((input) => input.split(" ").map((a) => +a)); const bfs = (queue) => { const dx = [0, 0, -1, 1]; const dy = [1, -1, 0, 0]; let head = 0; while (queue.length > head) { const [x, y] = queue[head++]; // 상하좌우 for (let i = 0; i < 4; i++) { const nx = x + dx[i]; const ny = y + dy[i]; if (nx >= 0 && nx < N && ny >= 0 && ny < M && board[nx][ny] === 0) { board[nx][ny] = board[x][y] + 1; queue.push([nx, ny]); } } } }; let temp = []; for (let i = 0; i < N; i++) { for (let j = 0; j < M; j++) { if (board[i][j] === 1) { temp.push([i, j]); } } } bfs(temp); let max = 0; for (let i = 0; i < N; i++) { for (let j = 0; j < M; j++) { if (board[i][j] === 0) { console.log("-1"); return; } if (max < board[i][j]) max = board[i][j]; } } console.log(max - 1);
민수 풀이
let [n, ...arr] = require('fs').readFileSync(__dirname + "/../input.txt").toString().trim().split("\n"); // let [n, ...arr] = require('fs').readFileSync("/dev/stdin").toString().trim().split("\n"); arr = arr.map(e => e.trim().split(' ').map(Number)); const [M, N] = n.split(' ').map(Number); const dx = [0, 0, -1, 1]; const dy = [-1, 1, 0, 0]; let stack = []; arr.forEach((e, i) => { e.forEach((r, j) => { if(r === 1) stack.push([j, i]) }) }) let cnt = 0; while(stack.length !== 0){ const currentStack = [...stack]; stack = []; for(let i = 0; i < currentStack.length; i++){ const [x, y] = currentStack[i]; for(let r = 0; r < 4; r++){ let mx = x + dx[r]; let my = y + dy[r]; if(mx >= 0 && my >= 0 && mx < M && my < N && arr[my][mx] === 0){ arr[my][mx] = 1; stack.push([mx, my]); } } } if(stack.length == 0) break; cnt += 1; } arr.forEach(e => { e.forEach(r => { if(r === 0) cnt = -1; }) }) console.log(cnt);
재현 풀이
송희 풀이
const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; let [sizes, ...tomato] = fs.readFileSync(filePath).toString().trim().split("\n"); const [m, n] = sizes.split(" "); const tomatoes = tomato.map((v, i) => v.split(" ")); class Node { constructor(x, y, count) { this.x = x; this.y = y; this.count = count; this.next = null; } } class Queue { constructor() { this.head = null; this.tail = null; this.size = 0; } push(node) { if (!this.head) this.head = node; if (this.tail) this.tail.next = node; this.tail = node; this.size++; } shift() { if (!this.size) return null; const node = this.head; this.head = this.head.next; this.size--; return node; } } const queue = new Queue(); const xArr = [-1, 1, 0, 0]; const yArr = [0, 0, -1, 1]; let unripeTomato = 0; let finalDays = 0; for (let y = 0; y < +n; y++) { for (let x = 0; x < +m; x++) { if (tomatoes[y][x] == 1) queue.push(new Node(x, y, 0)); else if (tomatoes[y][x] == 0) unripeTomato++; } } while (queue.size != 0) { const node = queue.shift(); const x = node.x; const y = node.y; const count = node.count; for (let i = 0; i < 4; i++) { const newX = x + yArr[i]; const newY = y + xArr[i]; if (tomatoes[newY] && tomatoes[newY][newX] == 0) { queue.push(new Node(newX, newY, count + 1)); tomatoes[newY][newX] = 1; unripeTomato--; finalDays = Math.max(finalDays, count + 1); } } } if (unripeTomato != 0) return console.log(-1); console.log(finalDays);