これがPython CHP 4による符号化テストである.実施3.ゲーム開発


N, M = map(int, input().split())
A, B, d = map(int, input().split())

# 게임 맵 정보 입력
game_map = []
for i in range(N):
    game_map.append(list(map(int, input().split())))

dir = [0, 3, 2, 1]  # 왼쪽으로 도니까 북서남동으로 입력
dA = [0, -1, 0, 1] # 행에서 이동좌표
dB = [-1, 0, 1, 0] # 열에서 이동좌표

game_map[A][B] = 1 # 현재 좌표 방문 처리
count = 1 # 육지 밟은 칸 개수
turn_time = 0 # 회전수

while True:
    d -= 1
    if d < 0: # 만약 다 돌았다면 서쪽으로  
        d = 3
    nA = A + dA[d] #nA에 A 이동좌표 저장
    nB = B + dB[d] #nB에 B 이동좌표 저장

    if game_map[nA][nB] == 0: # 입력받은 맵 0인 경우
        game_map[nA][nB] = 1 # 방문 표시
        A = nA
        B = nB
        count += 1 # 방문했으니 1증가
        turn_time = 0 # 회전수 초기화
        continue
    else:
        turn_time += 1
        
    if turn_time == 4: #회전수 4인 경우 뒤로 한칸
        nA = A - dA[d] # 이전 A좌표
        nB = B - dB[d] # 이전 B좌표
        if game_map[nA][nB] == 0: # 뒤로 갈 수 있다면
            A = nA
            B = nB
        else:
            break
        turn_time = 0
print(count)
  • 例回答
  • n, m = map(int, input().split())
    
    # 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
    d = [[0] * m for _ in range(n)]
    x, y, direction = map(int(input().split()))
    d[x][y] = 1
    
    # 전체 맵 정보를 입력받기
    array = []
    for i in range(n):
        array.append(list(map(int, input().split())))
    
    # 북, 동, 남, 서 방향 정의
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    
    # 왼쪽으로 회전
    def turn_left():
        global direction
        direction -= 1
        if direction == -1:
            direction = 3
    
    # 시뮬레이션 시작
    count = 1
    turn_time = 0
    while True:
        # 왼쪽으로 회전
        turn_left()
        nx = x + dx[direction]
        ny = y + dy[direction]
        # 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
        if d[nx][ny] == 0 and array[nx][ny] == 0:
            d[nx][ny] = 1
            x = nx
            y = ny
            count += 1
            turn_time = 0
            continue
        # 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
        else:
            turn_time += 1
        # 네 방향 모두 갈 수 없는 경우
        if turn_time == 4:
            nx = x - nx[direction]
            ny = y - ny[direction]
            # 뒤로 갈 수 있다면 이동하기
            if array[nx][ny] == 0:
                x = nx
                y = ny
            # 뒤가 바다로 막혀있는 경우
            else:
                break
            turn_time = 0
    print(count)
    ->わあ...本当に問題から理解できなくて時間がかかりすぎて….