[BaekJoon] ๋ฐฑ์ค 3190๋ฒ : ๋ฑ
Updated:
3190๋ฒ : ๋ฑ
์ ์ ํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ๋ ๋ฌธ์ ์ด๋ค.
๋ฑ์ ์์น๋ฅผ ์ ์ฅํ๊ธฐ ์ํด deque ๋ฅผ ์ฌ์ฉํ๋ค.
์ฌ๊ณผ๋ฅผ ๋จน๋ ๊ฒฝ์ฐ ๋จธ๋ฆฌ๋ง ์ถ๊ฐํด ์ฃผ๋ฉด ๋๊ณ ์ฌ๊ณผ๊ฐ ์๋ ๊ฒฝ์ฐ ๋จธ๋ฆฌ๋ฅผ ์ถ๊ฐํด ์ค ๋ค popleft ๋ฅผ ํตํด ๊ผฌ๋ฆฌ๋ฅผ ๋ผ ์ค๋ค.
๋ฐฉํฅ์ ๋ฐ๊พธ๋ ๊ฒฝ์ฐ๋ ํ์ฌ ์งํ ์ค์ธ ๋ฐฉํฅ์ ๋ฐ๋ผ ์ผ์ชฝ ์ค๋ฅธ์ชฝ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๊ตฌ๋ถํด ์ฃผ์๋ค.
[2021.03.02] ๋ณต์ต
import sys
from collections import deque
def move(field, snake):
next_i, next_j, time, head = snake[-1]
if head == 'U':
next_i -= 1
if head == 'R':
next_j += 1
if head == 'D':
next_i += 1
if head == 'L':
next_j -= 1
if 0 <= next_i < len(field) and 0 <= next_j < len(field) and field[next_i][next_j] <= 1:
# ์ด๋ ํ ๊ณณ์ ์ฌ๊ณผ๊ฐ ์๋ ๊ฒฝ์ฐ
if field[next_i][next_j] != 1:
tail = snake.popleft()
field[tail[0]][tail[1]] = 0
field[next_i][next_j] = 9
snake.append([next_i, next_j, time + 1, head])
return True
else:
return False
def solution(field, orders):
# i, j, time, head
snake = deque([[0, 0, 0, 'R']])
field[0][0] = 9
sec = 0
while True:
sec += 1
if not move(field, snake):
return sec
if orders:
if snake[-1][2] == orders[-1][0]:
time, dire = orders.pop()
if snake[-1][3] == 'U':
if dire == 'D':
snake[-1][3] = 'R'
elif dire == 'L':
snake[-1][3] = 'L'
elif snake[-1][3] == 'R':
if dire == 'D':
snake[-1][3] = 'D'
elif dire == 'L':
snake[-1][3] = 'U'
elif snake[-1][3] == 'D':
if dire == 'D':
snake[-1][3] = 'L'
elif dire == 'L':
snake[-1][3] = 'R'
elif snake[-1][3] == 'L':
if dire == 'D':
snake[-1][3] = 'U'
elif dire == 'L':
snake[-1][3] = 'D'
if __name__ == '__main__':
n = int(input())
field = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(int(input())):
i, j = map(int, sys.stdin.readline().rsplit())
field[i - 1][j - 1] = 1
orders = []
for _ in range(int(input())):
order = list(sys.stdin.readline().rsplit())
order[0] = int(order[0])
orders.append(order)
orders.sort(reverse=True)
print(solution(field, orders))
from collections import deque
def solution():
global arr
snake_body = deque([[0, 0]])
snake_dir = deque([[0, 0, 'right']])
sec = 1
while True:
head_loc = []
snake_head = snake_dir[-1]
if move_list and int(move_list[0][0]) == sec - 1: # ๋ฐฉํฅ์ ํ์ ํด์ผํ๋ ๊ฒฝ์ฐ
if snake_head[2] == 'right':
if move_list[0][1] == 'D':
head_loc = [snake_head[0] + 1, snake_head[1], 'down']
elif move_list[0][1] == 'L':
head_loc = [snake_head[0] - 1, snake_head[1], 'up']
elif snake_head[2] == 'down':
if move_list[0][1] == 'D':
head_loc = [snake_head[0], snake_head[1] - 1, 'left']
elif move_list[0][1] == 'L':
head_loc = [snake_head[0], snake_head[1] + 1, 'right']
elif snake_head[2] == 'left':
if move_list[0][1] == 'D':
head_loc = [snake_head[0] - 1, snake_head[1], 'up']
elif move_list[0][1] == 'L':
head_loc = [snake_head[0] + 1, snake_head[1], 'down']
elif snake_head[2] == 'up':
if move_list[0][1] == 'D':
head_loc = [snake_head[0], snake_head[1] + 1, 'right']
elif move_list[0][1] == 'L':
head_loc = [snake_head[0], snake_head[1] - 1, 'left']
move_list.popleft()
else: # ์ง์งํ๋ ๊ฒฝ์ฐ
if snake_head[2] == 'up':
head_loc = [snake_head[0] - 1, snake_head[1], 'up']
elif snake_head[2] == 'right':
head_loc = [snake_head[0], snake_head[1] + 1, 'right']
elif snake_head[2] == 'down':
head_loc = [snake_head[0] + 1, snake_head[1], 'down']
elif snake_head[2] == 'left':
head_loc = [snake_head[0], snake_head[1] - 1, 'left']
if (head_loc[:2] in snake_body or
head_loc[0] < 0 or head_loc[0] >= arr_size or
head_loc[1] < 0 or head_loc[1] >= arr_size):
return sec
if head_loc[:2] in apple_loc:
apple_loc.remove(head_loc[:2])
snake_dir.append(head_loc)
snake_body.append(head_loc[:2])
else:
snake_dir.popleft()
snake_body.popleft()
snake_dir.append(head_loc)
snake_body.append(head_loc[:2])
sec += 1
if __name__ == '__main__':
arr_size = int(input())
arr = [[0] * arr_size for _ in range(arr_size)]
apple_cnt = int(input())
apple_loc = [list(map(int, input().rsplit())) for _ in range(apple_cnt)]
for i in range(apple_cnt):
apple_loc[i][0] -= 1
apple_loc[i][1] -= 1
move_cnt = int(input())
move_list = deque([list(input().rsplit()) for _ in range(move_cnt)])
print(solution())
Leave a comment