티스토리 뷰

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

 

12100번: 2048 (Easy)

첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2

www.acmicpc.net

 

구현. 완전탐색

 

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v => v.split(' ').map(Number))
const [N] = input[0];
const board = input.splice(1);

const rotate = (board) => {
  let newBoard = Array.from(Array(N), () => Array(N).fill(0))
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < N; j++) {
      newBoard[j][i] = board[i][j]
    }
  }
  return newBoard
}


const goUp = (board) => {
  board = rotate(board);
  let newBoard = [];
  for (let i = 0; i < N; i++) {
    let now = board[i].filter(v => v != 0);
    for (let j = 1; j < now.length; j++) {
      if (now[j] == now[j - 1]) {
        now[j - 1] *= 2;
        now[j] = 0;
      }
    }
    now = now.filter(v => v != 0);
    while (now.length < N) {
      now.push(0)
    }
    newBoard.push(now)
  }
  return rotate(newBoard);
}

const goDown = (board) => {
  board = rotate(board);
  let newBoard = [];
  for (let i = 0; i < N; i++) {
    let now = board[i].reverse().filter(v => v != 0);
    for (let j = 1; j < now.length; j++) {
      if (now[j] == now[j - 1]) {
        now[j - 1] *= 2;
        now[j] = 0;
      }
    }
    now = now.filter(v => v != 0);
    while (now.length < N) {
      now.push(0)
    }
    newBoard.push(now.reverse())
  }
  return rotate(newBoard);

}

const goLeft = (board) => {
  let newBoard = [];
  for (let i = 0; i < N; i++) {
    let now = board[i].filter(v => v != 0);
    for (let j = 1; j < now.length; j++) {
      if (now[j] == now[j - 1]) {
        now[j - 1] *= 2;
        now[j] = 0;
      }
    }
    now = now.filter(v => v != 0);
    while (now.length < N) {
      now.push(0)
    }
    newBoard.push(now)
  }
  return newBoard;
}

const goRight = (board) => {
  let newBoard = [];
  for (let i = 0; i < N; i++) {
    let now = board[i].reverse().filter(v => v != 0);
    for (let j = 1; j < now.length; j++) {
      if (now[j] == now[j - 1]) {
        now[j - 1] *= 2;
        now[j] = 0;
      }
    }
    now = now.filter(v => v != 0);
    while (now.length < N) {
      now.push(0)
    }
    newBoard.push(now.reverse())
  }
  return newBoard;
}

const solution = (N, map) => {
  let q = [];
  let answer = 0;
  q.push([map, 0])
  while (q.length > 0) {
    const [board, cnt] = q.shift();
    if (cnt == 5) {
      for (let i = 0; i < N; i++) {
        for (let j = 0; j < N; j++) {
          if (answer < board[i][j]) {
            answer = board[i][j];
          }
        }
      }

    } else {
      q.push([goUp([...board]), cnt + 1])
      q.push([goDown([...board]), cnt + 1])
      q.push([goLeft([...board]), cnt + 1])
      q.push([goRight([...board]), cnt + 1])
    }

  }

  console.log(answer)
}

solution(N, board);
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함