

백준 뱀 문제는 2차원 배열로 하지 않아도 큐를 활용해서 풀 수 있는 문제다
deque으로 표현하여 양쪽에서 삽입/삭제deque.popleft() 생략apple_coord에서 제거current에 있는 좌표와 새 머리 위치를 비교하여 자충돌 여부를 판단import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
apple = int(input())
apple_coord = [tuple(map(int, input().split())) for _ in range(apple)]
l = int(input())
direction = [input().strip().split() for _ in range(l)]
current = deque([(1,1)])
dir = 0 # 0 오른쪽 방향, 1 아래, 2 왼쪽, 3 위
dx = [0, 1, 0, -1] # 오른쪽, 아래, 왼쪽, 위
dy = [1, 0, -1, 0]
time = 0
idx = 0
while True:
time += 1
head_x, head_y = current[-1]
# 새로운 머리 위치 계산
new_x = head_x + dx[dir]
new_y = head_y + dy[dir]
# 2. 충돌 체크 (범위를 벗어났을 때, 내 몸통에 닿았을 때)
if new_x < 1 or new_x > n or new_y < 1 or new_y > n: # n 범위를 벗어나거나, 음수일 떄
break
if (new_x, new_y) in current:
break
# 3. 새 머리 추가
current.append((new_x, new_y))
# 4. 사과 체크
if (new_x, new_y) in apple_coord:
# 사과 먹음 → 꼬리 안 자름
apple_coord.remove((new_x, new_y))
else:
# 사과 안 먹음 → 꼬리 자름
current.popleft()
# 5. 방향 전환 체크
if idx < l and time == int(direction[idx][0]):
if direction[idx][1] == 'D':
dir = (dir + 1) % 4 # 오른쪽 회전
else: # 'L'
dir = (dir - 1) % 4 # 왼쪽 회전
idx += 1
print(time)
/: 실수 나눗셈 //: 정수 나눗셈 (몫)%: 나머지%파이썬에서 나머지는 다음 공식을 만족:
a == (a // b) * b + (a % b)
즉, 나머지 %의 결과는 나눗는 수 b의 부호를 따름
print(7 % 3) # 1
print(-7 % 3) # 2
print(7 % -3) # -2
print(-7 % -3) # -1
각 예제의 계산 과정을 보면:
-7 // 3 == -3
-3 * 3 = -9
-7 - (-9) = 2 → -7 % 3 == 2
7 // -3 == -3
-3 * -3 = 9
7 - 9 = -2 → 7 % -3 == -2
-7 // -3 == 2
2 * -3 = -6
-7 - (-6) = -1 → -7 % -3 == -1
C나 Java에서는 %는 피제수 a의 부호를 따라감. 반면, 파이썬에서는 나눗는 수 b의 부호를 따라감.
printf("%d\n", -7 % 3); // -1 (C)
print(-7 % 3) # 2 (Python)
이 차이를 이해하고 있어야 다중 언어를 사용할 때 오류를 피할 수 있음
abs(b)를 사용:positive_mod = a % abs(b)
a % b 또는 math.fmod(a, b) 사용 고려% 연산자는 항상 나눗는 수 b의 부호를 따름.a == (a // b) * b + (a % b)
왕꿈틀이처럼 생겼당