[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())



Categories:

Updated:

Leave a comment