뱀
코드
import sys
from collections import deque
def move(visited, corner):
time = 0
snake = deque()
snake.append((1, 1))
dir = 1
move_dic = {1 : [0, 1], 2 : [1, 0], 3 : [0, -1], 4 : [-1, 0]}
while corner:
cor, d = corner.popleft()
cor = int(cor)
while cor != time:
head_x = snake[-1][0]
head_y = snake[-1][1]
next_head = (head_x + move_dic[dir][0], head_y + move_dic[dir][1])
if 0 < next_head[0] < n + 1 and 0 < next_head[1] < n + 1:
if visited[next_head[0]][next_head[1]] == 1:
visited[next_head[0]][next_head[1]] = 0
snake.append((next_head[0], next_head[1]))
else:
if next_head in snake:
return print(time + 1)
else:
snake.popleft()
snake.append((next_head[0], next_head[1]))
time += 1
else:
return print(time + 1)
if d == "D":
dir += 1
if dir == 5:
dir = 1
else:
dir -= 1
if dir == 0:
dir = 4
while True:
head_x = snake[-1][0]
head_y = snake[-1][1]
next_head = (head_x + move_dic[dir][0], head_y + move_dic[dir][1])
if 0 < next_head[0] < n + 1 and 0 < next_head[1] < n + 1:
if visited[next_head[0]][next_head[1]] == 1:
visited[next_head[0]][next_head[1]] = 0
snake.append((next_head[0], next_head[1]))
else:
if next_head in snake:
return print(time + 1)
else:
snake.append((next_head[0], next_head[1]))
snake.popleft()
time += 1
else:
return print(time + 1)
if __name__ == '__main__':
n = int(sys.stdin.readline())
visited = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
apple = deque()
a = int(sys.stdin.readline())
for i in range(a):
apple.append(list(map(int, sys.stdin.readline().split())))
b = int(sys.stdin.readline())
corner = deque()
for j in range(b):
corner.append(list(sys.stdin.readline().split()))
for i in range(n + 1):
for j in range(n + 1):
if [i, j] in apple:
visited[i][j] = 1
else:
visited[i][j] = 0
move(visited, corner)
시험 감독
코드
import sys
n = int(sys.stdin.readline())
student = list(map(int, sys.stdin.readline().split()))
b, c = map(int, sys.stdin.readline().split())
count = 0
for i in student:
if i <= b:
count += 1
else:
if (i - b) % c == 0:
count += (i - b) // c + 1
else:
count += (i - b) // c + 2
print(count)