[알고리즘] 2048(Easy)

Gomi·2021년 8월 1일
0

백준 삼성 SW 역량 테스트 기출 문제

2048(Easy)

백준 12100 2048(Easy)

2048게임을 시뮬레이션 하여 최대 5번 움직였을 때 최대값을 탐색하는 문제이다. 게임 특성상 회차를 거듭하여 최대값이 떨어지는 경우는 없기에 5개를 뽑는 경우의 수만 생각하면 된다. 따라서 상, 하, 좌, 우 네 가지 동작을 중복을 허용하고, 순서를 구분하여 다섯개를 뽑는 경우의 수인 중복 순열을 이용하여 문제를 풀 수 있겠다는 생각을 사전에 할 수 있다.

[상, 하, 좌, 우]를 각각 [0, 1, 2, 3]으로 생각한다면[0,0,0,0,0]~[3,3,3,3,3]까지 경우를 검사하여 최대값을 가지는 블럭을 선별한다.

경우의 수를 봤을 때 쉽게 떠올릴 수 있는 itertools 라이브러리의 중복순열 메소드인 product를 사용하고, 이를 시뮬레이션하는 함수를 구현하여야 한다. 시뮬레이션 함수는 구현 영역의 문제라 빠르게 해결하기 위해서는 경험치가 필요한 것으로 보인다. 풀고나면 설명할 것도 없을 정도로 단순하긴한데.... 너무 오래 걸렸다.

통과 코드

from collections import deque
from copy import deepcopy
from itertools import product

size = int(input())
orgboard = []
for i in range(size):
   orgboard.append(list(map(int, input().split())))

def move(moveList):     #0123(상하좌우)에 따라 보드를 움직이고 가장 큰 수를 반환
   board = deepcopy(orgboard)
   for move in moveList:
       if move == 0:
           for i in range(size):
               temp = []
               for j in range(0, size):
                   if board[j][i]!=0:
                       temp.append(board[j][i])
                       board[j][i]=0
               for k in range(len(temp)-1):
                   if temp[k] == temp[k+1]:
                       temp[k] += temp[k+1]
                       temp[k+1] = 0
               index = 0
               for l in temp:
                   if l!=0:
                       board[index][i]=l
                       index+=1
       elif move == 1:
           for i in range(size):
               temp = []
               for j in range(size-1,-1,-1):
                   if board[j][i]!=0:
                       temp.append(board[j][i])
                       board[j][i]=0
               for k in range(len(temp)-1):
                   if temp[k] == temp[k+1]:
                       temp[k] += temp[k+1]
                       temp[k+1] = 0
               index = size-1
               for l in temp:
                   if l!=0:
                       board[index][i]=l
                       index-=1
       elif move == 2:
           for i in range(size):
               temp = []
               for j in range(0, size):
                   if board[i][j]!=0:
                       temp.append(board[i][j])
                       board[i][j]=0
               for k in range(len(temp)-1):
                   if temp[k] == temp[k+1]:
                       temp[k] += temp[k+1]
                       temp[k+1] = 0
               index = 0
               for l in temp:
                   if l!=0:
                       board[i][index]=l
                       index+=1
       else:
           for i in range(size):
               temp = []
               for j in range(size-1,-1,-1):
                   if board[i][j]!=0:
                       temp.append(board[i][j])
                       board[i][j]=0
               for k in range(len(temp)-1):
                   if temp[k] == temp[k+1]:
                       temp[k] += temp[k+1]
                       temp[k+1] = 0
               index = size-1
               for l in temp:
                   if l!=0:
                       board[i][index]=l
                       index-=1
   maxnum = 0
   for i in board:
       if maxnum < max(i):
           maxnum = max(i)

   return maxnum

case = list(product([0,1,2,3],repeat=5))   #0,1,2,3에서 5개 중복순열 추출한 경우의 수
answer = 0
answercase = 0
for i in case:
   temp = move(i)
   if temp>answer:
       answer = temp
       answercase = i

print(answer)
profile
터키어 배운 롤 덕후

0개의 댓글