๐ŸŽฒ ๋ฐฑ์ค€ 7562๋ฒˆ ๋‚˜์ดํŠธ์˜ ์ด๋™

Jeongeunยท2023๋…„ 6์›” 19์ผ
0

๋ฐฑ์ค€

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

๋ฐฑ์ค€ 7562๋ฒˆ

๐Ÿ’Š ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ willCheck์— pushํ• ๋•Œํ•ด์ฃผ์–ด์•ผ ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();

const dir = [
  [2, -1],
  [2, 1],
  [-2, -1],
  [-2, 1],
  [1, -2],
  [1, 2],
  [-1, -2],
  [-1, 2],
];
for (let i = 0; i < N * 3; i += 3) {
  const size = +input[i];
  const chess = Array.from(new Array(size), () => new Array(size).fill(0));
  const [currentY, currentX] = input[i + 1].split(" ").map(Number);
  const [endY, endX] = input[i + 2].split(" ").map(Number);

  let willCheck = [[currentY, currentX, 0]];
  while (willCheck.length) {
    const [pointY, pointX, count] = willCheck.shift();
    if (pointY === endY && pointX === endX) {
      console.log(count);
      break;
    }
    for (let i = 0; i < 8; i++) {
      const nextY = pointY + dir[i][0];
      const nextX = pointX + dir[i][1];
      const nextCount = count + 1;

      if (
        nextY >= 0 &&
        nextX >= 0 &&
        nextY < size &&
        nextX < size &&
        chess[nextY][nextX] === 0
      ) {
        chess[nextY][nextX] = 1;
        willCheck.push([nextY, nextX, nextCount]);
      }
    }
  }
}

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