-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
71 lines (62 loc) · 1.68 KB
/
Copy pathMaze.cpp
File metadata and controls
71 lines (62 loc) · 1.68 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
#include <bits/stdc++.h>
using namespace std;
bool vis[1001][1001];
int dis[1001][1001];
vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int n, m;
char a[1001][1001];
pair<int, int> parent[1001][1001];
bool valid(int i, int j){
if (i < 0 || i >= n || j < 0 || j >= m || a[i][j] == '#') return false;
return true;
}
bool bfs(int sx, int sy, int ex, int ey) {
queue<pair<int, int>> q;
q.push({sx, sy});
vis[sx][sy] = true;
while (!q.empty()) {
pair<int, int> par = q.front();
q.pop();
if (par.first == ex && par.second == ey){
while (par.first != sx || par.second != sy){
a[par.first][par.second] = 'X';
par = parent[par.first][par.second];
}
a[sx][sy] = 'R';
a[ex][ey] = 'D';
return true;
}
for (int i = 0; i < 4; i++) {
int ci = par.first + d[i].first;
int cj = par.second + d[i].second;
if (valid(ci, cj) && !vis[ci][cj]){
q.push({ci, cj});
vis[ci][cj] = true;
parent[ci][cj] = par;
}
}
}
return false;
}
int main()
{
cin >> n >> m;
int sx,sy,ex,ey;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> a[i][j];
if(a[i][j] == 'R') sx = i, sy = j;
if(a[i][j] == 'D') ex = i, ey = j;
}
}
memset(vis, false, sizeof(vis));
memset(dis, -1, sizeof(dis));
bfs(sx, sy, ex, ey);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << a[i][j];
}
cout << endl;
}
return 0;
}