-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[백준 알고리즘] 미로탐색.txt
78 lines (63 loc) · 1.46 KB
/
[백준 알고리즘] 미로탐색.txt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//미로 탐색
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef struct way{
int r, c;
int step;
}Way;
int rdir[4] = {-1,0,1,0};
int cdir[4] = {0,1,0,-1};
int n,m;
int result =999999;
int arr[101][101];
bool visit[101][101];
bool canGo(int r, int c){
if(r<0 || r>=n || c<0 || c>=m){
return false;
}
else if(visit[r][c] == true){
return false;
}
else if(arr[r][c] == 0){
return false;
}
else if(arr[r][c] == 1){
return true;
}
return false;
}
int main(){
scanf("%d %d", &n, &m);
for(int i =0; i<n; i++){
string temp;
cin >> temp;
for(int j =0; j<temp.size(); j++){
arr[i][j] = temp[j] - '0';
}
}
queue<Way> root;
root.push({0,0,1});
visit[0][0] = true;
while(!root.empty()){
Way temp = root.front();
root.pop();
// printf("%d %d\n", temp.r, temp.c);
if(temp.r == n-1 && temp.c == m-1){
result = min(result,temp.step);
}
else{
for(int i =0; i<4; i++){
if(canGo(temp.r+rdir[i], temp.c+cdir[i])){
visit[temp.r+rdir[i]][temp.c+cdir[i]] = true;
root.push({temp.r+rdir[i], temp.c+cdir[i], temp.step+1});
}
}
}
}
printf("%d", result);
return 0;
}