Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 백준 21606번
- 동적 프로그래밍(Dynamic Programming)
- 이분 탐색(Binary Search)
- 백준 17608번
- 큐(Queue)
- BFS(Breadth First Search)
- 분할 정복(Divide and Conquer)
- 위상 정렬(Topological Sort)
- 다익스트라 알고리즘(Dijkstra Algorithm)
- 스택(Stack)
- 위상 정렬(Topology Sort)
- 그리디 알고리즘(Greedy Algorithm)
- 백준 2504번
- 백준 1707번
- 백준 2261번
- 백준 2493번
- 알고리즘 개념
- 그래프(Graph)
- DFS(Depth First Search)
- 백준 9012번
- 트리(Tree)
- 백준 10000번
- 이분 그래프(Bipartite Graph)
- DFS & BFS
- BFS
- 백준 2812번
- 백준 1948번
- 플로이드 워셜 알고리즘(Floyd-Warshall Algorithm)
- DFS
- 백준 18352번
Archives
- Today
- Total
Always Be Wise
큐 : 뱀(백준 3190번) 본문
728x90
▶ 문제 : https://www.acmicpc.net/problem/3190
3190번: 뱀
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임
www.acmicpc.net
##### 문제 #####
# 사과의 위치와 뱀의 이동경로가 주어질 때 이 게임이 몇 초에 끝나는지 계산하라.
##### 입력 #####
# 첫째 줄에 보드의 크기 N이 주어진다. (2 ≤ N ≤ 100)
# 다음 줄에 사과의 개수 K가 주어진다. (0 ≤ K ≤ 100)
# 다음 K개의 줄에는 사과의 위치가 주어지는데, 첫 번째 정수는 행, 두 번째 정수는 열 위치를 의미한다.
# 사과의 위치는 모두 다르며, 맨 위 맨 좌측 (1행 1열) 에는 사과가 없다.
# 다음 줄에는 뱀의 방향 변환 횟수 L 이 주어진다. (1 ≤ L ≤ 100)
# 다음 L개의 줄에는 뱀의 방향 변환 정보가 주어지는데, 정수 X와 문자 C로 이루어져 있으며,
# 게임 시작 시간으로부터 X초가 끝난 뒤에 왼쪽(C가 'L') 또는 오른쪽(C가 'D')로 90도 방향을 회전시킨다는 뜻이다.
# X는 10,000 이하의 양의 정수이며, 방향 전환 정보는 X가 증가하는 순으로 주어진다.
##### 출력 #####
# 첫째 줄에 게임이 몇 초에 끝나는지 출력한다.
▶ 접근 방법
▶ 풀이 코드
import sys
from collections import deque
board_size = int(input())
apples = int(input())
# 벽 : 1, 이동 가능 공간 : 0
board = [[1] * (board_size + 2)] + [[1] + [0] * board_size + [1] for _ in range(board_size)] + [[1] * (board_size +2)]
# 사과 놓인 곳 : 2
for _ in range(apples):
x, y = map(int, sys.stdin.readline().split())
board[x][y] = 2
switching_num = int(input())
switching_info = deque(sys.stdin.readline().split() for _ in range(switching_num))
time_count = 0
# 현재 위치
start_x, start_y = 1, 1
# 이동 방향
direction = {0:(-1,0), 1:(0,1), 2:(1,0), 3:(0,-1)} ## 0:북 1:동 2:남 3:서
# 현재 방향 : 동(오른쪽)
current_dir = 1
snake = deque([[1, 1]])
# 뱀의 위치 : 3
board[1][1] = 3
while True :
start_x = start_x + direction[current_dir][0]
start_y = start_y + direction[current_dir][1]
if board[start_x][start_y] == 2 :
board[start_x][start_y] = 3
snake.append([start_x, start_y])
time_count += 1
elif board[start_x][start_y] == 0 :
board[start_x][start_y] = 3
snake.append([start_x, start_y])
del_x, del_y = snake.popleft()
board[del_x][del_y] = 0
time_count += 1
else :
time_count += 1
break
if len(switching_info) != 0:
if int(switching_info[0][0]) == time_count :
if switching_info[0][1] == 'L':
current_dir = (current_dir-1) % 4
elif switching_info[0][1] == 'D':
current_dir = (current_dir+1) % 4
switching_info.popleft()
print(time_count)
▶ 관련 링크
2021.11.18 - [알고리즘] - 큐(Queue)란?
2021.11.18 - [알고리즘] - 큐 : 요세푸스 문제 0(백준 11866번)
2021.11.18 - [알고리즘] - 큐 : 카드2(백준 2164번)
2021.11.18 - [알고리즘] - 큐 : 큐2(백준 18258번)
'알고리즘 > 백준' 카테고리의 다른 글
트리 : 이진 검색 트리(백준 5639번) (0) | 2021.11.19 |
---|---|
트리 : 트리 순회(백준 1991번) (0) | 2021.11.19 |
큐 : 요세푸스 문제 0(백준 11866번) (0) | 2021.11.18 |
큐 : 카드2(백준 2164번) (0) | 2021.11.18 |
큐 : 큐2(백준 18258번) (0) | 2021.11.18 |
Comments