-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210226_BOJ_2178.cpp
More file actions
53 lines (40 loc) · 858 Bytes
/
Copy path210226_BOJ_2178.cpp
File metadata and controls
53 lines (40 loc) · 858 Bytes
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
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int N, M;
int v[101][101]{ 0 };
int main() {
cin >> N >> M;
for (int i = 1; i <= N; ++i) {
string a; cin >> a;
for (int j = 1; j <= M; ++j) {
v[i][j] = a[j - 1] - '0';
}
}
int dir[4][2]{ {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
queue<pair<int, pair<int, int>>> q;
q.push({ 1, {1, 1} });
v[1][1] = 0;
while (!q.empty()) {
int x = q.front().second.first;
int y = q.front().second.second;
int cnt = q.front().first;
q.pop();
if (x == N && y == M) {
cout << cnt << "\n";
return 0;
}
for (int i = 0; i < 4; ++i) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (!(nx >= 0 && nx <= N && ny >= 0 && ny <= M))
continue;
if (v[nx][ny] == 0)
continue;
q.push({ cnt + 1, {nx, ny} });
v[nx][ny] = 0;
}
}
return 0;
}