[백준 14562] 태권왕 with node.js

waterglasses·2021년 11월 10일
0

📌 문제

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

📌 풀이

  • BFS로 간단히 풀이한다
  • 내가 포인트를 2배씩 얻으면 상대방이 3점씩 얻는 것을 queue에 넣는다.
  • 두번째는 내가 포인트를 1점씩 얻고 상대방은 그대로인 점수를 queue에 넣는다.

📌 코드

const fs = require('fs');
const stdin = (
  process.platform === 'linux'
    ? fs.readFileSync('/dev/stdin').toString().trim()
    : `6
10 20
2 7
15 62
10 37
11 50
34 59`
).split('\n');

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

const getCntOfKick = (S, T) => {
  const queue = [];
  queue.push([S, T, 0]);

  while (queue.length) {
    const [myPoint, opponentPoint, cntOfKick] = queue.shift();
    if (myPoint <= opponentPoint) {
      queue.push([myPoint * 2, opponentPoint + 3, cntOfKick + 1]);
      queue.push([myPoint + 1, opponentPoint, cntOfKick + 1]);

      if (myPoint === opponentPoint) return cntOfKick;
    }
  }
};

const TEST_CASE = parseInt(input());
for (let i = 0; i < TEST_CASE; i++) {
  const [S, T] = input().split(' ').map(Number);

  const cntOfKick = getCntOfKick(S, T);
  console.log(cntOfKick);
}
profile
매 순간 성장하는 개발자가 되려고 노력하고 있습니다.

0개의 댓글