💡문제접근
- 방향 배열과 방향값을 설정해서 방향을 판단했다.
- 반시계 방향으로 회전하는 경우 역순으로 진행되므로 북(0)에서 서(3)으로 가는 경우를 잘 고려해야한다. 따라서 반시계 방향으로 90도 회전하는 경우를
d = (d+3) % 4
로 맞춰주었다.
- 시계 방향으로 회전하는 경우 서(3)에서 북(0)으로 가는 경우를 잘 고려해야 한다. 따라서 시계 방향으로 90도 회전하는 경우를
d = (d + 1) % 4
로 맞춰주었다.
💡코드(메모리 : 31120KB, 시간 : 2788ms)
import sys
input = sys.stdin.readline
T = int(input())
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
for _ in range(T):
operation = list(input().strip())
x, y = 0, 0
direction = 0
minx, miny, maxx,maxy = 0, 0, 0, 0
for op in operation:
if op == "F":
x += dx[direction]
y += dy[direction]
elif op == "B":
x -= dx[direction]
y -= dy[direction]
elif op == "L":
direction = (direction + 3) % 4
elif op == "R":
direction = (direction + 1) % 4
minx = min(minx, x)
maxx = max(maxx, x)
miny = min(miny, y)
maxy = max(maxy, y)
print((maxx - minx) * (maxy - miny))
💡소요시간 : 24m