728x90
반응형
https://www.acmicpc.net/problem/1987
1987번: 알파벳
세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으
www.acmicpc.net
<풀이>
시간을 줄이는 것이 제일 큰 숙제였다.
pop, push 작업이 많은 경우 list를 통해 구현하는 것 보다 deque를 이용하는 것이 월등히 빠르다.
<전체 코드>
import sys
from collections import deque
row, col = map(int, sys.stdin.readline().split())
graph = []
for _ in range(row):
graph.append(list(sys.stdin.readline()[:-1]))
drow = [-1, 0, 1, 0]
dcol = [0, 1, 0, -1]
q = deque([[0, 0, graph[0][0], 1]])
ans = 0
while q:
now_row, now_col, visit, cnt = q.popleft()
for i in range(4):
next_row = now_row+drow[i]
next_col = now_col+dcol[i]
if next_row >= row or next_row < 0 or next_col >= col or next_col < 0:
continue
next = graph[next_row][next_col]
if next not in visit:
q.appendleft([next_row, next_col, visit+next, cnt+1])
ans = max(ans, cnt)
print(ans)
728x90
반응형
'# Coding > # 백준' 카테고리의 다른 글
[백준 / 1997] 최소 스패닝 트리 - Python (0) | 2022.01.10 |
---|---|
[백준 / 2206] 벽 부수고 이동하기 - Python (0) | 2021.12.30 |
[백준 / 3190] 뱀 - Python (0) | 2021.12.23 |
[백준 / 15686] 치킨 배달 - Python (0) | 2021.12.21 |
[백준 / 12865] 평범한 배낭 - Python (0) | 2021.12.17 |