-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-4.cpp
More file actions
72 lines (58 loc) · 1.24 KB
/
5-4.cpp
File metadata and controls
72 lines (58 loc) · 1.24 KB
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
/**
* @file 5-4.cpp
* @brief 미로 탈출
* @author Sam Kim (samkim2626@gmail.com)
*/
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
int n, m;
int graph[201][201];
pair<int, int> d[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int bfs(int x, int y)
{
pair<int, int> xy = {x, y};
queue<pair<int, int>> q;
q.push(xy);
while (!q.empty())
{
x = q.front().first;
y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + d[i].first;
int ny = y + d[i].second;
if (nx < 1 || nx > n || ny < 1 || ny > m)
{
continue;
}
if (graph[nx][ny] == 0)
{
continue;
}
if (graph[nx][ny] == 1)
{
graph[nx][ny] = graph[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
}
return graph[n][m];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%1d", &graph[i][j]);
}
}
cout << bfs(1, 1) << ' ';
return 0;
}