백준 12100번 2048 (Easy) (Python 3)

2022. 9. 13. 18:06Problem solving

 

Baekjoon online judge 12100번 파이썬

 

링크

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

 

12100번: 2048 (Easy)

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

www.acmicpc.net

반례 모음

https://www.acmicpc.net/board/view/61812

 

글 읽기 - <<12100번 반례모음>>

댓글을 작성하려면 로그인해야 합니다.

www.acmicpc.net

브루트포스,구현문제 dfs를 이용해 풀었다.

소스코드

import sys
from collections import deque
input=sys.stdin.readline

def up(map_data):
    queue=deque()
    now_max=-1
    new_map_data = [[0 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        for j in range(n):

            if map_data[j][i]!=0:
                queue.append(map_data[j][i])

        index=0
        while queue:
            now=queue.popleft()
            if queue and queue[0]==now:
                queue.popleft()
                new_map_data[index][i] = now*2
                index += 1
                now_max=max(now*2,now_max)
            else:
                new_map_data[index][i]=now
                index+=1
    return new_map_data,now_max

def down(map_data):
    queue=deque()
    now_max=-1
    new_map_data = [[0 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        for j in range(n-1,-1,-1):

            if map_data[j][i]!=0:
                queue.append(map_data[j][i])

        index=n-1
        while queue:
            now=queue.popleft()
            if queue and queue[0]==now:
                queue.popleft()
                new_map_data[index][i] = now * 2
                index -= 1
                now_max=max(now*2,now_max)
            else:
                new_map_data[index][i]=now
                index-=1
    return new_map_data,now_max

def left(map_data):
    queue=deque()
    new_map_data=[[0 for _ in range(n)] for _ in range(n)]
    now_max=-1
    for j in range(n):
        for i in range(n):

            if map_data[j][i]!=0:
                queue.append(map_data[j][i])

        index=0
        while queue:
            now=queue.popleft()
            if queue and queue[0]==now:
                queue.popleft()
                new_map_data[j][index] = now * 2
                index += 1
                now_max=max(now*2,now_max)
            else:
                new_map_data[j][index]=now
                index+=1
    return new_map_data,now_max

def right(map_data):
    queue=deque()
    now_max=-1
    new_map_data = [[0 for _ in range(n)] for _ in range(n)]
    for j in range(n):
        for i in range(n-1,-1,-1):

            if map_data[j][i]!=0:
                queue.append(map_data[j][i])
        index=n-1
        while queue:
            now=queue.popleft()
            if queue and queue[0]==now:
                queue.popleft()
                new_map_data[j][index] = now * 2
                index -= 1
                now_max=max(now*2,now_max)
            else:
                new_map_data[j][index]=now
                index-=1
    return new_map_data,now_max



function_call=[up,left,right,down]

def dfs(depth,ans,map_data):
    global out_now_max
    if depth==5:
        out_now_max=max(out_now_max,ans)
        return

    for f in function_call:
        new_map_data,ans=f(map_data)
        #print(map_data,ans,f,depth)
        out_now_max = max(out_now_max, ans)
        dfs(depth+1,ans,new_map_data)


n=int(input())
map_data=[list(map(int,input().split())) for _ in range(n)]
out_now_max=max(map(max,map_data))
dfs(0,-1,map_data)
print(out_now_max)