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
- GroupBy
- 백준
- 우선순위큐
- 전처리
- 그리디
- 누적합
- 건축물대장정보
- 유사도
- 코사인유사도
- pandas
- 그래프탐색
- dp
- xmltodict
- cosine
- 유클리드
- 분할정복
- geopy
- 너비우선탐색
- 깊이우선탐색
- Geocoding
- NLP
- 공공데이터
- TF-IDF
- 그래프이론
- 구현
- 자연어처리
- 지진대피소
- 수학
- 재귀
- 비트마스킹
Archives
- Today
- Total
정리용
[백준 2178] 파이썬 - 미로탐색 본문
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
1. 코드설명
n,m=map(int,input().split())
arr = [list(map(int, input())) for _ in range(n)]
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
done = [[0] * m for _ in range(n)]
done[0][0] = 1 # 시작점 = 0, 0
from collections import deque
q = deque([(0, 0)])
while q:
x, y = q.popleft()
#print ('x, y = ' , x,y )
#for k in range(n):
# print(done[k])
# dx,dy 를 이용한 상하좌우 탐색
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if done[nx][ny] == 0 and arr[nx][ny] == 1:
done[nx][ny] = done[x][y] + 1
# q.append 를 함으로써 q 대기열에 넣어 while 문을 지속시킴
q.append((nx, ny))
# 목적지에 도달하면 답을 출력
if x == n-1 and y == m-1:
print(done[x][y])
2. 주의사항
deque 에 두 영역에 대한 값이 들어오면
대기열에 쌓이게 되고 먼저 입력된 정보부터 처리하게된다.
'알고리즘 > 백준' 카테고리의 다른 글
[백준 2667] 파이썬 - 단지번호 (0) | 2021.12.16 |
---|---|
[백준 11727] 파이썬 - 2xn 타일링 2 (0) | 2021.12.15 |
[백준 10844] (0) | 2021.12.13 |
[백준 2156] 포도주 시식 (0) | 2021.12.11 |
[백준 1912] 파이썬 - 연속합 (0) | 2021.12.10 |
Comments