|
1 | 1 | /*
|
2 | 2 | implementation of BFS on 2D grid
|
3 | 3 | @author Amirul islam :: Nov 1, 2019
|
| 4 | + @modified Amirul islam :: Jan 24, 2021 |
4 | 5 | _
|
5 | 6 | _|_ o._ o._ __|_| _. _|_
|
6 | 7 | _>| ||| ||| |(_|| |(_|_>| |
|
|
9 | 10 |
|
10 | 11 | #include <bits/stdc++.h>
|
11 | 12 | using namespace std;
|
12 |
| -#define pii pair <int, int> |
13 |
| -#define mp make_pair |
14 |
| -#define isInside(x, y, row, col) (x >= 0 && x < row && y >= 0 && y < col) |
15 | 13 |
|
16 | 14 | const int mx = 1e3;
|
17 | 15 |
|
18 |
| -int dx[] = {-1, 0, 1, 0}; |
19 |
| -int dy[] = {0, 1, 0, -1}; |
20 |
| - |
21 |
| -char cell[mx][mx]; |
22 |
| -bool vis[mx][mx]; |
| 16 | +char grid[mx][mx]; |
| 17 | +int vis [mx][mx]; |
23 | 18 | int dis[mx][mx];
|
24 | 19 |
|
| 20 | +int dx[] = {0, 1, 0, -1}; |
| 21 | +int dy[] = {1, 0, -1, 0}; |
| 22 | + |
| 23 | +bool isInside(int x, int y, int row, int col) { |
| 24 | + return x >= 0 && x < row && y >= 0 && y < col; |
| 25 | +} |
| 26 | + |
25 | 27 | void bfs(int sx, int sy, int row, int col) {
|
26 |
| - vis[sx][sy] = 1; |
27 |
| - queue <pii> q; |
28 |
| - q.push(mp(sx, sy)); |
| 28 | + vis[sx][sy] = true; |
| 29 | + queue < pair <int, int> > q; |
| 30 | + q.push(make_pair(sx, sy)); |
| 31 | + |
29 | 32 | while (!q.empty()) {
|
30 |
| - pii top = q.front(); |
| 33 | + pair <int, int> top = q.front(); |
31 | 34 | q.pop();
|
| 35 | + |
32 | 36 | for (int k = 0; k < 4; k++) {
|
33 | 37 | int nx = top.first + dx[k];
|
34 | 38 | int ny = top.second + dy[k];
|
35 |
| - if (isInside(nx, ny, row, col) && cell[nx][ny] != '#' && !vis[nx][ny]) { |
36 |
| - vis[nx][ny] = 1; |
37 |
| - dis[nx][ny] = dis[top.first][top.second] + 1; |
38 |
| - q.push(mp(nx, ny)); |
39 |
| - } |
| 39 | + |
| 40 | + if (isInside(nx, ny, row, col) && |
| 41 | + grid[nx][ny] != '#' && !vis[nx][ny]) { |
| 42 | + vis[nx][ny] = true; |
| 43 | + dis[nx][ny] = dis[top.first][top.second] + 1; |
| 44 | + q.push(make_pair(nx, ny)); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void printGrid(int row, int col) { |
| 51 | + for (int i = 0; i < row; i++) { |
| 52 | + for (int j = 0; j < col; j++) { |
| 53 | + cout << dis[i][j] << " "; |
40 | 54 | }
|
| 55 | + cout << "\n"; |
41 | 56 | }
|
42 | 57 | }
|
43 | 58 |
|
44 | 59 | int main() {
|
45 |
| - //freopen("in", "r", stdin); |
46 |
| - //ios_base::sync_with_stdio(0); |
47 |
| - //cin.tie(NULL); |
| 60 | + freopen("in", "r", stdin); |
| 61 | + |
| 62 | + int row, col; |
| 63 | + cin >> row >> col; |
48 | 64 |
|
49 |
| - int row, col, sx, sy, cnt(0); |
50 |
| - scanf("%d %d", &row, &col); |
51 | 65 | swap(row, col);
|
| 66 | + |
52 | 67 | for (int i = 0; i < row; i++) {
|
53 |
| - scanf("%s", cell[i]); |
| 68 | + cin >> grid[i]; |
54 | 69 | }
|
| 70 | + |
55 | 71 | bfs(0, 0, row, col);
|
56 |
| - for (int i = 0; i < row; i++) { |
57 |
| - for (int j = 0; j < col; j++) printf("%d ", dis[i][j]); |
58 |
| - printf("\n"); |
59 |
| - } |
| 72 | + |
| 73 | + printGrid(row, col); |
| 74 | + |
60 | 75 | return 0;
|
61 | 76 | }
|
62 | 77 |
|
63 | 78 | /*
|
64 |
| -
|
65 | 79 | Input:
|
66 |
| -
|
67 | 80 | 3 3
|
68 | 81 | ...
|
69 | 82 | #.#
|
70 | 83 | ...
|
71 |
| -
|
72 | 84 | Output:
|
73 |
| -
|
74 | 85 | 0 1 2
|
75 | 86 | 0 2 0
|
76 | 87 | 4 3 4
|
77 |
| -
|
78 | 88 | */
|
0 commit comments