[BOJ] 8911: 거북이 (Python)

토즐라·2022년 5월 24일
0

문제 링크

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


풀이 전략

Implementation

어렵지 않은 구현 문제입니다.
거북이 로봇에게 내릴 수 있는 명령은 다음과 같이 네 가지가 있습니다.

  1. F: 한 눈금 앞으로

  2. B: 한 눈금 뒤로

  3. L: 왼쪽으로 90도 회전

  4. R: 오른쪽으로 90도 회전

각각의 케이스에 대해서 거북이의 이동좌표를 모두 포함하는 직사각형의 넓이를 저장해놓고, 그 최솟값을 출력하면 답을 구할 수 있습니다.


구현

n = int(input())

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

for i in range(n):
	# 현재 거북이의 위치
    pos_x = 0
    pos_y = 0
    
    # 거북이가 바라보고 있는 방향 (init: 북쪽)
    pos_dir = 0
    
    move = list(input())
    trace = [(pos_x, pos_y)]
    
    for j in move:
        if j == 'F':
            pos_x = pos_x + dx[pos_dir]
            pos_y = pos_y + dy[pos_dir]
        elif j == 'B':
            pos_x = pos_x - dx[pos_dir]
            pos_y = pos_y - dy[pos_dir]
        elif j == 'R':
            if pos_dir == 3:
                pos_dir = 0
            else:
                pos_dir += 1
        
        elif j == 'L':
            if pos_dir == 0:
                pos_dir = 3
            else:
                pos_dir -= 1

        trace.append((pos_x, pos_y))
    width = max(trace, key = lambda x:x[0])[0] - min(trace, key = lambda x:x[0])[0]
    height = max(trace, key = lambda x:x[1])[1] - min(trace, key = lambda x:x[1])[1]
    print(width * height)
profile
Work Hard, Play Hard 🔥🔥🔥

0개의 댓글