SWEA 5650 핀볼 게임(with Python)

daeungdaeung·2021년 10월 20일
0
  • 시뮬레이션 문제인데 엄청 어려웠습니다.
import sys
sys.stdin = open(r'/Users/kangdaeyoung/Downloads/sample_input (6).txt', 'r')

# 우선 여기서는 위로 움직이면 0, 하: 2, 좌: 1, 우: 3 으로 설정

change_directions = [
    [0, 2, 3, 1, 2, 2],
    [1, 0, 2, 3, 3, 3],
    [2, 3, 0, 0, 1, 0],
    [3, 1, 1, 2, 0, 1],
]

directions = [
    [-1, 0],
    [0, -1],
    [1, 0],
    [0, 1]
]

T = int(input())

for tc in range(1, T+1):
    N = int(input())
    brd = [list(map(int, input().split())) for _ in range(N)]

    max_score = 0

    # [블랙홀]
    # [시작점 후보 location]
    # [웜홀-쌍]
    # 위의 내용을 저장해야한다.
    black_halls = []
    start_locations = []
    wormholes = {}

    for r in range(N):
        for c in range(N):
            if brd[r][c] == -1:
                black_halls.append([r, c])
            elif brd[r][c] == 0:
                start_locations.append([r, c])
            elif 6 <= brd[r][c] <= 10:
                wormhole_number = brd[r][c]
                if wormholes.get(wormhole_number):
                    wormholes[wormhole_number].append([r, c])
                else:
                    wormholes[wormhole_number] = [[r, c]]

    # 핀볼 시뮬레이션
    for origin_r, origin_c in start_locations:
        for d_idx in range(4):
            cr, cc = origin_r, origin_c
            origin_d_idx = d_idx
            cnt = 0
            while True:
                # STEPS
                # 1. 현재 위치에서 방향 전환 or 웜홀 처리
                block_idx = brd[cr][cc]
                if 0 <= block_idx <= 5:
                    if 1 <= block_idx <= 5:
                        cnt += 1
                    d_idx = change_directions[d_idx][block_idx]
                elif 6 <= block_idx <= 10:
                    if wormholes[block_idx][0] == [cr, cc]:
                        cr, cc = wormholes[block_idx][1]
                    else:
                        cr, cc = wormholes[block_idx][0]

                # 2. 처리된 방향으로 전진
                dr, dc = directions[d_idx]

                if 0 <= cr+dr < N and 0 <= cc+dc < N:
                    cr, cc = cr+dr, cc+dc
                else:
                    # 벽 만난 경우
                    cnt += 1
                    d_idx = (d_idx+2)%4

                if (origin_r == cr and origin_c == cc) or brd[cr][cc] == -1:
                    break
            if max_score < cnt:
                max_score = cnt

    print(f'#{tc} {max_score}')
profile
개발자가 되고싶읍니다...

0개의 댓글