[백준 7562] 나이트의 이동 with node.js

waterglasses·2021년 10월 29일
0

📌 문제링크

https://www.acmicpc.net/problem/7562

📌 풀이

  • 나이트가 갈 수 있는 좌표를 dx, dy로 설정해 놓는다.
  • 시작하는 좌표에서 도착하는 좌표까지의 값을 cntOfDistance에 누적해서 계산한다.

📌 코드

const fs = require('fs');
const stdin = (
  process.platform === 'linux'
    ? fs.readFileSync('/dev/stdin').toString().trim()
    : `3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1`
).split('\n');

const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

const getMinDistance = (N, [startX, startY], [endX, endY]) => {
  const cntOfDistance = Array.from(Array(N), () => Array(N).fill(0));
  const dx = [2, 1, -2, -1, -2, -1, 1, 2];
  const dy = [1, 2, 1, 2, -1, -2, -2, -1];
  let queue = [[startX, startY]];
  let queueCursor = 0;

  while (queue.length > queueCursor) {
    let [x, y] = queue[queueCursor++];

    if (x === endX && y === endY) return cntOfDistance[x][y];

    for (let i = 0; i < 8; i++) {
      let nx = x + dx[i];
      let ny = y + dy[i];

      if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
      if (cntOfDistance[nx][ny]) continue;

      cntOfDistance[nx][ny] = cntOfDistance[x][y] + 1;
      queue.push([nx, ny]);
    }
  }
};

const TEST_CASE = Number(input());
for (let i = 0; i < TEST_CASE; i++) {
  const lenOfChessBoard = Number(input());
  const startPoint = input().split(' ').map(Number);
  const endPoint = input().split(' ').map(Number);

  const cntOfMinDistanceInChessboard = getMinDistance(lenOfChessBoard, startPoint, endPoint);
  console.log(cntOfMinDistanceInChessboard);
}
profile
매 순간 성장하는 개발자가 되려고 노력하고 있습니다.

0개의 댓글