Skip to content

Commit 2086de5

Browse files
committed
[BOJ] #3190. 뱀 / 골드4 / 60분 / 힌트->성공
1 parent b96ecdf commit 2086de5

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
'''
6+
내용 정리하면서 최적화한 코드
7+
'''
8+
# 1. 입력
9+
N = int(input()) # 보드 크기
10+
K = int(input()) # 사과 개수
11+
12+
# 사과 위치 입력
13+
apples = set()
14+
for _ in range(K):
15+
x, y = map(int, input().split())
16+
apples.add((x - 1, y - 1)) # 0-index로 변환
17+
18+
L = int(input()) # 방향 변환 횟수
19+
turn_times = deque([]) # 방향 전환 정보 / [(3, 'D'), (15, 'L'), (17, 'D')]
20+
for _ in range(L):
21+
t, d = input().split()
22+
turn_times.append((int(t), d))
23+
24+
# 2. 초기화
25+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 방향 리스트 (우, 하, 좌, 상) # 방향 변환에 따라 인덱싱
26+
snake = deque([(0, 0)]) # 뱀 초기 위치: 큐
27+
current = 0 # 초기 방향. (오른쪽)
28+
time = 0
29+
30+
# 3. 시뮬레이션
31+
while True:
32+
time += 1
33+
34+
# 3.1. 뱀 머리 이동
35+
head_X, head_Y = snake[-1] # 머리 좌표
36+
dx, dy = directions[current] # 오른쪽 좌표
37+
new_head = (head_X + dx, head_Y + dy)
38+
39+
# 3.2. 벽이나 자기 몸에 닿았다면 게임을 종료
40+
if not (0 <= new_head[0] < N and 0 <= new_head[1] < N) or new_head in snake:
41+
print(time) # 게임 종료
42+
break
43+
44+
# 3.3. 사과 확인 하고 꼬리를 처리
45+
snake.append(new_head) # 새로운 머리 추가
46+
47+
# 사과 있는지 확인
48+
if new_head in apples:
49+
apples.remove(new_head) # 사과 제거
50+
else:
51+
snake.popleft() # 꼬리 제거
52+
53+
# 3.4. 방향 전환
54+
if turn_times and time == turn_times[0][0]:
55+
_, direction = turn_times.popleft()
56+
if direction == 'D': # 오른쪽
57+
current = (current + 1) % 4
58+
elif direction == 'L': # 왼쪽
59+
current = (current + 3) % 4
60+
61+
'''
62+
# 문제 푸는 당시에 작성했던 코드
63+
import sys
64+
from collections import deque
65+
66+
input = sys.stdin.readline
67+
68+
N = int(input()) # 보드 크기
69+
K = int(input()) # 사과 개수
70+
board = [[0] * N for _ in range(N)] # 보드 판
71+
72+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 방향 리스트 (우, 하, 좌, 상) # 방향 변환에 따라 인덱싱
73+
snake = deque([(0, 0)]) # 뱀 초기 위치: 큐
74+
current = 0 # 현재 방향은 오른쪽
75+
time = 0
76+
direction_info = deque([]) # 방향 정보 / [(3, 'D'), (15, 'L'), (17, 'D')]
77+
78+
# 사과 위치 입력, 배치
79+
for _ in range(K):
80+
x, y = map(int, input().split())
81+
board[x - 1][y - 1] = 1
82+
83+
L = int(input()) # 방향 변환 횟수
84+
for _ in range(L):
85+
t, d = map(str, input().split())
86+
direction_info.append((int(t), d))
87+
88+
89+
while True:
90+
time += 1
91+
92+
# 뱀 머리 이동
93+
head_X, head_Y = snake[-1] # 머리 좌표
94+
dx, dy = directions[current] # 오른쪽 좌표
95+
new_head = (head_X + dx, head_Y + dy)
96+
97+
# 벽이나 자기 몸에 닿았는지 판단한다.
98+
if (0 <= new_head[0] < N and 0 <= new_head[1] < N) and new_head not in snake:
99+
snake.append(new_head) # 새로운 머리 추가
100+
101+
# 사과 있는지 확인
102+
# 있으면 꼬리 냅두고,
103+
if board[new_head[0]][new_head[1]] == 1:
104+
board[new_head[0]][new_head[1]] = 0 # 사과 먹음
105+
106+
# 없으면 꼬리를 줄인다.
107+
else:
108+
snake.popleft()
109+
110+
# 방향 바꾸기
111+
if direction_info and time == direction_info[0][0]:
112+
t, dir = direction_info.popleft()
113+
if dir == 'D': # 오른쪽
114+
current = (current + 1) % 4
115+
elif dir == 'L': # 왼쪽
116+
current = (current + 3) % 4
117+
118+
else:
119+
# 게임 종료
120+
print(time)
121+
break
122+
'''

0 commit comments

Comments
 (0)