일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- swea
- Database
- network
- D3
- 운영체제
- 법의학
- 문제풀이
- 백준
- BOJ
- 알고리즘
- algorithm
- algogritim
- 자료구조
- data structure
- cs
- 프로그래머스
- SW Expert Academy
- D2
- 독서
- 네트워크
- 감상문
- Computer Science
- c++
- db
- LeetCode
- language
- 재테크/투자
- OS
- 데이터베이스
- Programmers
Archives
- Today
- Total
선택은 나의 것
[BOJ 백준] 2178번 미로 탐색 (python) 본문
문제
BOJ 2178 : https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
접근
BFS를 통해 간단히 답을 구할 수 있다. 이때 임의의 칸(x, y)에 도착하는 가장 빠른 길이 아니라면 더이상 진행하지 않는 것이 시간을 줄이는 관건이다. 이는 visit 배열을 만들어 관리해 줄 수 있다.
코드
from collections import deque
n, m = map(int, input().split())
miro = []
for i in range(n):
s = input()
miro.append(s)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
visit = [[0 for col in range(110)] for row in range(110)]
dq = deque()
dq.append((0, 0))
visit[0][0] = 1
while True:
if len(dq) == 0:
break
x, y = dq.popleft()
for i in range(4):
newx = dx[i] + x
newy = dy[i] + y
if newx >= n or newx < 0 or newy >= m or newy < 0:
continue
if miro[newx][newy] == "1":
if visit[newx][newy] == 0:
visit[newx][newy] = visit[x][y] + 1
dq.append((newx, newy))
elif visit[newx][newy] != 0 and visit[newx][newy] > visit[x][y] + 1:
visit[newx][newy] = visit[x][y] + 1
dq.append((newx, newy))
print(visit[n - 1][m - 1])
'☽ Algorithm > BOJ' 카테고리의 다른 글
[BOJ 백준] 10769번 행복한지 슬픈지 (python) (0) | 2020.08.09 |
---|---|
[BOJ 백준] 11779번 최소비용 구하기 2 (0) | 2020.08.07 |
[BOJ 백준] 2799번 블라인드 (0) | 2020.08.06 |
[BOJ 백준] 1822번 차집합 (python) (0) | 2020.08.06 |
[BOJ 백준] 4344번 평균은 넘겠지 (python) (0) | 2020.08.04 |
Comments