[Boj] 거북 8911(python)
문제링크 : https://www.acmicpc.net/problem/8911
테스트 케이스 N이 주어지고, N줄 동안 method가 주어진다.
필자는 0-위 1-오 2-아 3-왼으로 관리했으며, 이차원 리스트로 설정하여 문제를 풀었다.
이차원 리스트 원소의 첫 항은 x축, 두 번째 항은 y축, 세 번째 항은 방향을 의미한다.
최종적으로 위치 리스트를 한 번은 x축을 기준으로, 한 번은 y축을 기준으로 정렬하여
직사각형의 면적을 구한다.
def go(position, command):
x = position[0]
y = position[1]
direction = position[2]
if command == 'F':
if position[2] == 0:
return [x, y+1, direction]
elif position[2] == 1:
return [x+1, y, direction]
elif position[2] == 2:
return [x, y-1, direction]
elif position[2] == 3:
return [x-1, y, direction]
elif command == 'B':
if position[2] == 0:
return [x, y-1, direction]
elif position[2] == 1:
return [x-1, y, direction]
elif position[2] == 2:
return [x, y+1, direction]
elif position[2] == 3:
return [x+1, y, direction]
elif command == 'L':
if position[2] == 0:
return [x, y, 3]
elif position[2] == 1:
return [x, y, 0]
elif position[2] == 2:
return [x, y, 1]
elif position[2] == 3:
return [x, y, 2]
elif command == 'R':
if position[2] == 0:
return [x, y, 1]
elif position[2] == 1:
return [x, y, 2]
elif position[2] == 2:
return [x, y, 3]
elif position[2] == 3:
return [x, y, 0]
N = int(input())
for _ in range(N):
methods = input()
positions = [[0, 0, 0]] # x, y, direction
for method in methods:
next_position = go(positions[-1], method)
positions.append(next_position)
positions.sort(key=lambda x:x[0])
min_X = positions[0]
max_X = positions[-1]
positions.sort(key=lambda x:x[1])
min_Y = positions[0]
max_Y = positions[-1]
print((max_X[0]-min_X[0]) * (max_Y[1] - min_Y[1]))
댓글남기기