백준 12100번 2048 (Easy) (Python 3)
2022. 9. 13. 18:06ㆍProblem 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)
'Problem solving' 카테고리의 다른 글
백준 19236번 청소년 상어 (Python 3) (0) | 2022.09.14 |
---|---|
백준 16939번 2×2×2 큐브 (Python 3) (0) | 2022.09.14 |
백준 12946번 육각 보드 (Python 3) (0) | 2022.09.08 |
백준 1208번 부분수열의 합 2 (Python 3) (0) | 2022.09.08 |
백준 1644번 소수의 연속합 (Python 3) (0) | 2022.09.08 |