KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • programmers
  • recursion
  • C
  • 다이나믹 프로그래밍
  • Level 1
  • 정렬
  • Level 2
  • Level1
  • codeup
  • Level 0
  • 수학
  • 그래프
  • string
  • C++
  • 문자열
  • 백준
  • 제코베 JS 100제
  • LeetCode
  • tree
  • js

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/백준

백준 / 그래프 / 14226번 / 이모티콘 / JS

2023. 12. 18. 17:31

< 문제 바로가기 >

 

14226번: 이모티콘

영선이는 매우 기쁘기 때문에, 효빈이에게 스마일 이모티콘을 S개 보내려고 한다. 영선이는 이미 화면에 이모티콘 1개를 입력했다. 이제, 다음과 같은 3가지 연산만 사용해서 이모티콘을 S개 만

www.acmicpc.net

 

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : '../input.txt';
// const filePath = process.platform === 'linux' ? '/dev/stdin' : 'BOJ/input.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const S = +input.shift();
const MAX = 2000 + 1;

const solution = () => {
  bfs();
};

const bfs = () => {
  const queue = [];
  const visited = Array.from({ length: MAX }, () => Array.from({ length: MAX }, () => false));

  // [이모티콘 개수, 클립보드에 복사된 이모티콘의 개수, 시간]
  queue.push([1, 0, 0]);

  while(queue.length > 0) {
    const [emoticon, clipboard, time] = queue.shift();

    // S개의 이모티콘을 만들었을 경우 출력하고 끝낸다.
    if(emoticon === S) {
      console.log(time);
      return;
    }
    if(visited[emoticon][clipboard] === true) {
      continue;
    }

    visited[emoticon][clipboard] = true;

    // 클립보드에 복사
    queue.push([emoticon, emoticon, time + 1]);
    // 붙여넣기
    if(emoticon + clipboard < MAX) {
      queue.push([emoticon + clipboard, clipboard, time + 1]);
    }
    // 삭제
    if(emoticon > 0) {
      queue.push([emoticon - 1, clipboard, time + 1]);
    }
  }
}

solution();
저작자표시 (새창열림)

'PS > 백준' 카테고리의 다른 글

백준 / 그래프 / 숨바꼭질 4 / JS  (0) 2023.12.21
백준 / 그래프 / 1697번 / 숨바꼭질 / JS  (0) 2023.12.19
백준 / 정렬 / 8979번 / 올림픽 / JS  (0) 2023.12.13
백준 / 백트래킹 / 18428번 / 감시 피하기 / JS  (2) 2023.12.12
    'PS/백준' 카테고리의 다른 글
    • 백준 / 그래프 / 숨바꼭질 4 / JS
    • 백준 / 그래프 / 1697번 / 숨바꼭질 / JS
    • 백준 / 정렬 / 8979번 / 올림픽 / JS
    • 백준 / 백트래킹 / 18428번 / 감시 피하기 / JS
    KimMinJun
    KimMinJun

    티스토리툴바