-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs2d.cpp
More file actions
31 lines (25 loc) · 759 Bytes
/
Copy pathbfs2d.cpp
File metadata and controls
31 lines (25 loc) · 759 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
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && mat[x][y] != '#' && dist[x][y] == inf;
}
void bfs2d(int start_x, int start_y) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
dist[i][j] = inf;
queue<pair<int, int>> qu;
qu.push({start_x, start_y});
dist[start_x][start_y] = 0;
while (!qu.empty()) {
auto [x, y] = qu.front();
qu.pop();
for (int i = 0; i < 4; ++i) {
int a = x + dx[i];
int b = y + dy[i];
if (valid(a, b)) {
dist[a][b] = dist[x][y] + 1;
qu.push(pair(a, b));
}
}
}
}