๐ ๋ฐฉ๋ฌธ์ฌ๋ถ๋ฅผ 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]);
}
}
}
}