-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ46.cpp
More file actions
47 lines (37 loc) · 1.2 KB
/
Copy pathQ46.cpp
File metadata and controls
47 lines (37 loc) · 1.2 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
#include <vector>
#include <queue>
using namespace std;
bool visited[101][101];
int solution(vector<vector<int>> maps) {
int n = maps.size();
int m = maps[0].size();
vector<vector<int>> directions {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
queue<pair<int, int>> dq;
dq.push(make_pair(0, 0));
maps[0][0] = 1;
visited[0][0] = true;
while (!dq.empty()) {
pair<int, int> cur = dq.front();
dq.pop();
for (const vector<int>& direction : directions) {
int row = cur.first + direction[0];
int col = cur.second + direction[1];
if (row < 0 || row >= n || col < 0 || col >= m) {
continue;
}
if (maps[row][col] == 0) {
continue;
}
if (visited[row][col]) {
continue;
}
if (row == n - 1 && col == m - 1) {
return maps[cur.first][cur.second] + 1;
}
visited[row][col] = true;
maps[row][col] = maps[cur.first][cur.second] + 1;
dq.push(make_pair(row, col));
}
}
return -1;
}