[백준 16432] 떡장수와 호랑이 with node.js

waterglasses·2021년 12월 27일
0

📌 문제

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

📌 풀이

  • 문제를 이해하는데에 많은 시간이 걸렸지만 이해하고 나면 많이 어렵지 않은 문제이다.
  • N일동안 daysToSellRiceCakes에 가지고가는 떡의 종류를 추가시켜놓고 day를 하나씩 증가시키면서 어제 먹은 떡이 아니면서 checkingIfEatenRiceCake를 체크해나가면서 계산하면 된다.

📌 코드

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

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

const printRiceCakesForTiger = (day, riceCakeEatenYesterday, eatenRiceCakes) => {
  if (day === N) {
    console.log(eatenRiceCakes.split('').join('\n'));
    process.exit();
  }

  for (let riceCake of daysToSellRiceCakes[day]) {
    if (riceCake !== riceCakeEatenYesterday && !checkingIfEatenRiceCake[day][riceCake]) {
      checkingIfEatenRiceCake[day][riceCake] = true;
      printRiceCakesForTiger(day + 1, riceCake, eatenRiceCakes + [riceCake]);
    }
  }
};

const N = parseInt(input());
const daysToSellRiceCakes = [];
const checkingIfEatenRiceCake = Array.from(new Array(N + 1), () => new Array(10).fill(false));
for (let i = 1; i <= N; i++) {
  const [numOfRiceCake, ...typeOfRiceCakes] = input().split(' ').map(Number);
  daysToSellRiceCakes.push(typeOfRiceCakes);
}

printRiceCakesForTiger(0, 0, []);
console.log(-1);
profile
매 순간 성장하는 개발자가 되려고 노력하고 있습니다.

0개의 댓글