-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
109 lines (92 loc) · 2 KB
/
Maze.cpp
File metadata and controls
109 lines (92 loc) · 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
bool vis[N][N];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
map<pair<int, int>, pair<int, int>> par;
int n, m;
char graph[N][N];
bool valid(int ci, int cj)
{
if (ci >= 0 && ci < n && cj >= 0 && cj < m && graph[ci][cj] != '#')
{
return true;
}
else
{
return false;
}
}
void bfs(int si, int sj)
{
vis[si][sj] = true;
queue<pair<int, int>> q;
q.push({si, sj});
while (!q.empty())
{
pair<int, int> node = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int ci = node.first + dx[i];
int cj = node.second + dy[i];
if (valid(ci, cj) && !vis[ci][cj])
{
vis[ci][cj] = true;
q.push({ci, cj});
par[{ci, cj}] = {node.first, node.second};
if (graph[ci][cj] == 'D')
{
return;
}
}
}
}
}
int main()
{
cin >> n >> m;
memset(vis, false, sizeof(vis));
pair<int, int> start, end;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> graph[i][j];
if (graph[i][j] == 'R')
{
start = {i, j};
}
if (graph[i][j] == 'D')
{
end = {i, j};
}
}
}
bfs(start.first, start.second);
if (vis[end.first][end.second])
{
int xi = end.first;
int xj = end.second;
while (graph[xi][xj] != 'R')
{
pair<int, int> p = par[{xi, xj}];
xi = p.first;
xj = p.second;
if (graph[xi][xj] != 'R')
{
graph[xi][xj] = 'X';
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << graph[i][j];
}
cout << endl;
}
return 0;
}