boj 23288 주사위굴리기2 (골드3)

김준오·2022년 4월 25일
0

알고리즘

목록 보기
86/91
post-thumbnail

문제

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

내 풀이

n,m,k = map(int,input().split())
arr = [list(map(int,input().split())) for _ in range(n)] ## 맵
dice = [0, 0, 0]  ## 주사위 좌표정보 y,x,dir
dice_state = [[0, 2, 0], [4, 1, 3], [0, 5, 0], [0, 6, 0]]  ## 주사위 형태
answer = 0  ## 누적 계산할 점

## 주사위 형태 ##
# 0 2 0
# 4 1 3
# 0 5 0
# 0 6 0

dy = [0, 1, 0, -1]  ## 방향 동:0, 남:1, 북:2, 서:3
dx = [1, 0, -1, 0]



def change_dice_state(direct):
    temp = dice_state[1][1]
    if direct == 0:
        dice_state[1][1] = dice_state[1][0]
        dice_state[1][0] = dice_state[3][1]
        dice_state[3][1] = dice_state[1][2]
        dice_state[1][2] = temp

    elif direct == 2:
        dice_state[1][1] = dice_state[1][2]
        dice_state[1][2] = dice_state[3][1]
        dice_state[3][1] = dice_state[1][0]
        dice_state[1][0] = temp

    elif direct == 1:
        dice_state[1][1] = dice_state[0][1]
        dice_state[0][1] = dice_state[3][1]
        dice_state[3][1] = dice_state[2][1]
        dice_state[2][1] = temp

    elif direct == 3:
        dice_state[1][1] = dice_state[2][1]
        dice_state[2][1] = dice_state[3][1]
        dice_state[3][1] = dice_state[0][1]
        dice_state[0][1] = temp


def move_dice():
    global dice
    y, x, direct = dice
    ny, nx = y + dy[direct], x + dx[direct]
    if ny < 0 or ny >= n or nx < 0 or nx >= m: ## 칸이 없으면 반대방향으로 굴러감
        direct = (direct+2) % 4
        ny, nx = y + dy[direct], x + dx[direct]

    change_dice_state(direct)   ## 주사위 굴러감에 따른 주사위 도면 변화
    dice = [ny, nx, direct]

def dfs(a,b,num) -> int:
    cnt = 1
    q = [(a, b)]
    visited = [(a, b)]

    while(q):
        y, x = q.pop()
        for i in range(4):
            ny, nx = y + dy[i], x + dx[i]
            if ny < 0 or ny >= n or nx < 0 or nx >= m \
                    or arr[ny][nx] != num or (ny, nx) in visited:
                continue

            q.append((ny, nx))
            visited.append((ny, nx))
            cnt += 1

    return cnt


def get_score(y, x):
    global answer
    num = arr[y][x]
    answer += (num * dfs(y, x, num))

def define_dir():
    global dice
    y, x, direct = dice
    down = dice_state[3][1]
    if down > arr[y][x]:
        direct = (direct + 1) % 4

    elif down < arr[y][x]:
        direct = (direct - 1) % 4

    dice[2] = direct

debug = False

for _ in range(k):
    move_dice()
    get_score(dice[0], dice[1])
    define_dir()

print(answer)

생각할점

주사위 좌표를 헷갈려서 그냥 보이는대로 2차원 배열로 만들었는데
사실 숫자 6개만 1차원배열로 만드는게 더 효율성 높은 풀이일것이다.
메모리나 시간복잡도 측면에서 크게 상관이 없기때문에 편의를위해 보이는대로 했다.

그외에는 설명대로 그대로 구현했다.
딱히 특별한 낚시 요소나 주의점은 없는것같다.

profile
jooooon

0개의 댓글

관련 채용 정보