Skip to content

Commit c08e8be

Browse files
authored
leehyeji319:: 3190_이혜지.py 추가
leehyeji319:: 3190_이혜지.py 추가
1 parent 2078b6f commit c08e8be

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

common/boj/3190/3190_이혜지.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from collections import deque
2+
3+
N = int(input())
4+
K = int(input())
5+
time = 0
6+
# 뱀이 이동할 판
7+
graph = [[0] * N for _ in range(N)]
8+
9+
# 사과 있는 부분 1로 설정
10+
for _ in range(K):
11+
r, c = map(int, input().split())
12+
graph[r - 1][c - 1] = 1
13+
14+
L = int(input())
15+
# 뱀의 방향 정보 (딕셔너리로)
16+
command = {}
17+
for _ in range(L):
18+
s, d = input().split()
19+
command[int(s)] = d
20+
21+
# 뱀의 몸정보 관리 큐
22+
snake = deque();
23+
snake.append((0, 0))
24+
25+
# 회전 정보 - 북 동 남 서
26+
dr = [-1, 0, 1, 0]
27+
dc = [0, 1, 0, -1]
28+
29+
# 현재 진행 방향 (처음은 동쪽으로 전진)
30+
current_dir = 1
31+
# 뱀의 처음 위치(0,0)을 2로 설정
32+
r, c = 0, 0
33+
graph[r][c] = 2
34+
35+
def turnSnake(next_dir):
36+
global current_dir
37+
if next_dir == 'L': #왼쪽회전
38+
current_dir = (current_dir - 1) % 4
39+
else: #오른쪽회전
40+
current_dir = (current_dir + 1) % 4
41+
42+
while True:
43+
time += 1
44+
r += dr[current_dir]
45+
c += dc[current_dir]
46+
47+
if r < 0 or r >= N or c < 0 or c >= N or (r, c) in snake: #범위체크랑 자기 몸에 부딪히는지 #맨첨에 뱀 몸 부딪히는 조건 안넣엇는데 통과함 머지??
48+
break
49+
50+
if graph[r][c] == 1: #사과가 있다면
51+
graph[r][c] = 2 #뱀늘어나~
52+
snake.append((r, c)) #큐에 몸정보 추가
53+
# 딕셔너리 키값 = 방향을 전환할 time시간
54+
if time in command: #전환할 시간이 된다면
55+
turnSnake(command[time]) #명령으로 회전한다
56+
elif graph[r][c] == 0: #사과없으면
57+
graph[r][c] = 2 #뱀그냥가
58+
snake.append((r, c)) #뱀몸추가해
59+
tr, tc = snake.popleft() #뱀 몸정보 지워주기
60+
graph[tr][tc] = 0 #한칸갓으니까 꼬리없애
61+
if time in command:
62+
turnSnake(command[time])
63+
else:
64+
break
65+
66+
print(time)
67+
# 사과 있는 칸 1 없는 칸 0 뱀2

0 commit comments

Comments
 (0)