파이썬 알고리즘 208번 | [백준 14499번] 주사위 굴리기 - 구현

Yunny.Log ·2022년 7월 21일
0

Algorithm

목록 보기
211/318
post-thumbnail

208. 주사위 굴리기

1) 어떤 전략(알고리즘)으로 해결?

2) 코딩 설명

<내 풀이>

풀이 참고, 아이디어 얻은 곳/ 출처 : https://esoongan.tistory.com/77


import sys

n,m, x,y, k = map(int, sys.stdin.readline().split())

# 1) 지도 기록
mapp =[]
for i in range(n) :
    mapp.append(list(map(int, sys.stdin.readline().split())))

# 2) 이동 방향 기록
loc = map(int, sys.stdin.readline().split())
dice = [0 for _ in range(6)]

# 4) 각 대응하는 밑-윗
pair = {0:5, 1:4, 2:3, 3:2, 4:1, 5:0} 

for l in loc : 

    if l==1: # 동 
        tmpx = x; tmpy=y+1

    elif l==2 : # 서
        tmpx = x; tmpy=y-1
        
    elif l==3 : # 북 
        tmpx = x-1; tmpy=y

    else : # 남 
        tmpx = x+1; tmpy=y

    if 0>tmpx or tmpx>=n  or 0>tmpy or tmpy>=m: 
        # 영역 벗어나면 걍 옮기지도 않고 다시 for 문으로 돌아가 
        continue

    else : 
        x=tmpx; y=tmpy
        if l==1: # 동 
            dice[0], dice[2],dice[3], dice[5] = dice[2], dice[5],dice[0], dice[3]
        elif l==2 : # 서
            dice[0], dice[2],dice[3], dice[5] = dice[3], dice[0],dice[5], dice[2]

        elif l==3 : # 북 
            dice[0], dice[1],dice[4], dice[5] = dice[1], dice[5],dice[0], dice[4]

        else : # 남 
            dice[0], dice[1],dice[4], dice[5] = dice[4], dice[0],dice[5], dice[1]
        
        if mapp[x][y]!=0 : 
            # 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사 & 칸에 쓰여 있는 수는 0
            dice[5] = mapp[x][y]
            mapp[x][y] = 0

        else : 
            # 주사위의 바닥면에 쓰여 있는 수가 칸에 복사
            mapp[x][y] = dice[5]

    print(dice[0])

<내 틀렸던 풀이, 문제점>

출처 : 출처


import sys

n,m, x,y, k = map(int, sys.stdin.readline().split())

# 1) 지도 기록
mapp =[]
for i in range(n) :
    mapp.append(list(map(int, sys.stdin.readline().split())))

# 2) 이동 방향 기록
loc = map(int, sys.stdin.readline().split())
dice = [0 for _ in range(6)]
bottom = 5
top = 0

# 3) 각 방향에 따른 주사위 밑

# 밑면이 1/6
ns1 = [[0,1,5,4],[5,1,0,4]] # 북이면 + 남이면 -
we1 = [[0,2,5,3],[5,2,0,3]] # 동이면 + 서면 -
# 밑면이 3,/4
ns2 = [[2,1,3,4],[3,1,2,4]]
we2 = [[2,5,3,0],[3,5,2,0]]
# 밑면이 2,5
ns3 = [[1,5,4,0],[4,0,1,5]]
we3 = [[1,2,4,3],[4,2,1,3]]

# 4) 각 대응하는 밑-윗
pair = {0:5, 1:4, 2:3, 3:2, 4:1, 5:0} 

for l in loc : 
    if l==1: # 동 
        tmpx = x; tmpy=y+1
        if bottom in [0,5] :
            if bottom == 0 :
                tmpbottom = we1[0][(we1[0].index(bottom)+1)%4]
            else : 
                tmpbottom = we1[1][(we1[1].index(bottom)+1)%4]

        elif bottom in [2,3] :
            if bottom == 2 :
                tmpbottom = we2[0][(we2[0].index(bottom)+1)%4]
            else : 
                tmpbottom = we2[1][(we2[1].index(bottom)+1)%4]
        else  : # [1,4]
            if bottom == 1 :
                tmpbottom = we3[0][(we3[0].index(bottom)+1)%4]
            else : 
                tmpbottom = we3[1][(we3[1].index(bottom)+1)%4]

    elif l==2 : # 서
        tmpx = x; tmpy=y-1
        if bottom in [0,5] :
            if bottom == 0 : 
                tmpbottom = we1[0][(we1[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = we1[1][(we1[1].index(bottom)-1)%4]

        elif bottom in [2,3] :
            if bottom == 2 : 
                tmpbottom = we2[0][(we2[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = we2[1][(we2[1].index(bottom)-1)%4]

        else  : # 1,4
            if bottom == 1 : 
                tmpbottom = we3[0][(we3[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = we3[1][(we3[1].index(bottom)-1)%4]
            
    elif l==3 : # 북 
        tmpx = x-1; tmpy=y
        if bottom in [0,5] :
            if bottom == 0 : 
                tmpbottom = ns1[0][(ns1[0].index(bottom)+1)%4]
            else  : 
                tmpbottom = ns1[1][(ns1[1].index(bottom)+1)%4]

        elif bottom in [2,3] :
            if bottom == 2 : 
                tmpbottom = ns2[0][(ns2[0].index(bottom)+1)%4]
            else  : 
                tmpbottom = ns2[1][(ns2[1].index(bottom)+1)%4]

        else  : # 1,4
            if bottom == 1 : 
                tmpbottom = ns3[0][(ns3[0].index(bottom)+1)%4]
            else  : 
                tmpbottom = ns3[1][(ns3[1].index(bottom)+1)%4]

    else : # 남 
        tmpx = x+1; tmpy=y
        if bottom in [0,5] :
            if bottom == 0 : 
                tmpbottom = ns1[0][(ns1[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = ns1[1][(ns1[1].index(bottom)-1)%4]

        elif bottom in [2,3] :
            if bottom == 2 : 
                tmpbottom = ns2[0][(ns2[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = ns2[1][(ns2[1].index(bottom)-1)%4]

        else  : # 1,4
            if bottom == 1 :
                tmpbottom = ns3[0][(ns3[0].index(bottom)-1)%4]
            else  : 
                tmpbottom = ns3[1][(ns3[1].index(bottom)-1)%4]

    if 0>tmpx or tmpx>=n  or 0>tmpy or tmpy>=m: 
        # 영역 벗어나면 걍 옮기지도 않고 다시 for 문으로 돌아가 
        continue
    else : 
        x=tmpx; y=tmpy; bottom = tmpbottom; top = pair[tmpbottom]

        if mapp[x][y]!=0 : 
            # 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사 & 칸에 쓰여 있는 수는 0
            dice[bottom] = mapp[x][y]
            mapp[x][y] = 0

        else : 
            # 주사위의 바닥면에 쓰여 있는 수가 칸에 복사
            mapp[x][y] = dice[bottom]
    print("11" , dice, "top", top+1, "bottom ", bottom+1)
    print(dice[top])

<반성 점>

  • 으으 경우 수 고려하는 것 실패 ㅠㅠ 흑흑

<배운 점>

0개의 댓글