algorithm/BOJ
BOJ 2178번 미로탐색
_JunHo
2020. 1. 14. 19:55
BOJ : https://www.acmicpc.net/problem/2178
GitHub : https://github.com/junho0956/Algorithm/blob/master/2178/2178/%EC%86%8C%EC%8A%A4.cpp
필요한 것은 현재 1 인지 0인지 확인할 배열, 현재까지 오는데 필요한 최소 횟수를 포함한 배열 2가지이다.
현재 접점에서 동서남북으로 갈 수 있는 곳을 체크하되,
만약 그곳이 방문했던 곳이라면 현재까지 오는데 필요한 횟수+1 과 방문확인하는 위치의 횟수를 비교해주는 작업으로
bfs 를 통해 탐색을 하면 된다.
더보기
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#pragma warning(disable:4996)
queue<pair<int, int> > q;
int mx[4] = { -1,1,0,0 };
int my[4] = { 0,0,1,-1 };
int N, M;
int arr[100][100];
int check[100][100];
int main() {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
for (int k = 0; k < M; k++) {
scanf("%1d", &arr[i][k]);
}
}
check[0][0] = 1;
q.push({ 0,0 });
while (!q.empty()) {
int y = q.front().first;
int x = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int yy = my[i] + y;
int xx = mx[i] + x;
if (yy >= 0 && xx >= 0 && yy < N && xx < M && arr[yy][xx]) {
if (check[yy][xx] == 0) {
q.push({ yy,xx });
check[yy][xx] = check[y][x] + 1;
}
else {
if (check[yy][xx] > check[y][x] + 1) {
q.push({ yy,xx });
check[yy][xx] = check[y][x] + 1;
}
}
}
}
}
printf("%d", check[N - 1][M - 1]);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|