http://acmicpc.net/problem/3190
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
K = int(input())
# 사과는 1로 표시
game_map = [[0]*N for _ in range(N)]
for _ in range(K):
x, y = map(int, input().split())
game_map[x-1][y-1] = 1
# 방향 전환 횟수 저장
L = int(input())
move = list()
for _ in range(L):
X, C = map(str, input().split())
X = int(X)
move.append((X, C))
# 뱀 위치
game_map[0][0] = 2
bam_head = (0, 0)
bam_tail = deque()
bam_tail.append((0, 0))
sec = 0
# 방향: 우(0), 하(1), 좌(2), 상(3)
x = [0, 1, 0, -1]
y = [1, 0, -1, 0]
# 시작은 오른쪽으로
cursor = 0
length = 1
gameover = False
prev = 0
for i in move:
cho, c = i[0], i[1]
for _ in range(cho - prev):
bam_head = (bam_head[0]+x[cursor], bam_head[1]+y[cursor])
sec += 1
# 게임 종료 조건 검사
if not (0 <= bam_head[0] < N and 0 <= bam_head[1] < N) or game_map[bam_head[0]][bam_head[1]] == 2:
gameover = True
break
else:
bam_tail.append(bam_head)
# 사과가 없으면 꼬리 줄이기
if game_map[bam_head[0]][bam_head[1]] != 1:
tail = bam_tail.popleft()
game_map[tail[0]][tail[1]] = 0
# 뱀 영역 표시
game_map[bam_head[0]][bam_head[1]] = 2
prev = cho
if gameover:
break
# 방향 전환
if c == 'L':
cursor = (cursor - 1) % 4
else:
cursor = (cursor + 1) % 4
# 마지막 방향전환을 할때까지 종료가 되지 않으면
# 끝까지 게임 진행
while not gameover:
bam_head = (bam_head[0]+x[cursor], bam_head[1]+y[cursor])
sec += 1
if not (0 <= bam_head[0] < N and 0 <= bam_head[1] < N) or game_map[bam_head[0]][bam_head[1]] == 2:
break
bam_tail.append(bam_head)
if game_map[bam_head[0]][bam_head[1]] != 1:
tail = bam_tail.popleft()
game_map[tail[0]][tail[1]] = 0
game_map[bam_head[0]][bam_head[1]] = 2
print(sec)'코딩테스트 > 코딩테스트 문제풀이' 카테고리의 다른 글
| [Python] 백준 <14500-테트로미노> (0) | 2025.07.17 |
|---|---|
| [Python] 백준 <2293-동전 1> (0) | 2025.07.16 |
| [Python] 백준 <17299-오등큰수> (0) | 2025.07.12 |
| [Python] 백준 <11054-가장 긴 바이토닉 부분 수열> (0) | 2025.07.11 |
| [Python] 백준 <16929-Two Dots> (0) | 2025.07.11 |