-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDSA.cpp
208 lines (203 loc) · 6.38 KB
/
DSA.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "bits/stdc++.h"
using namespace std;
struct DSU
{
vector<int> parent, sze;
void build(int n)
{
parent.resize(n, 0), sze.resize(n, -1);
for (int k = 0; k < n; k++)
parent[k] = k, sze[k] = 1;
}
int find(int v)
{
if (parent[v] == v)
return v;
else
return parent[v] = find(parent[v]);
}
void unite(int a, int b)
{
a = find(a), b = find(b);
if (a != b)
{
if (sze[a] < sze[b])
swap(a, b);
parent[b] = a, sze[a] += sze[b];
}
}
};
class Maze
{
public:
vector<vector<char>> maze;
int start;
int end;
int n;
void printmaze();
void buildmaze();
int AssignWeight();
Maze(int t);
private:
set<tuple<int, int, int>> edges; // {weight,cell1,cell2} edge between cell1 and cell2
set<pair<int, int>> mst; // {cell1,cell2} edge between cell1 and cell2
DSU dsu;
};
Maze::Maze(int t) //Generating maze with no cells connected yet
{
n = t;
maze.resize(2 * n + 1, vector<char>(2 * n + 1, '-'));
dsu.build(n * n + 1);
for (int i = 1; i <= 2 * n + 1; i += 2)
{
for (int j = 1; j <= 2 * n + 1; j += 2)
{
if (i != 2 * n + 1 && j != 2 * n + 1)
maze[i][j] = ' ';
if (j != 2 * n + 1)
maze[i - 1][j] = '-';
maze[i - 1][j - 1] = '+';
if (i != 2 * n + 1)
maze[i][j - 1] = '|';
}
}
}
void Maze::printmaze()
{
int st = 2 * start + 1, ed = 2 * end + 1;
while (st--)
cout << ' ';
cout << 'v' << endl;
for (int i = 0; i < 2 * n + 1; i++)
{
for (int j = 0; j < 2 * n + 1; j++)
cout << maze[i][j];
cout << endl;
}
while (ed--)
cout << ' ';
cout << 'v' << endl;
}
void Maze::buildmaze()
{
for (int i = 1; i <= n; i++) // cells are numbered from 1 to n*n
{
int f = (i - 1) * n;
for (int j = f + 1; j <= f + n; j++)
{
if (j < f + n)
{
int getWeight = AssignWeight();
edges.insert({getWeight, j, j + 1}); // Edge between current cell and right cell
}
if (j + n <= n * n)
{
int getWeight = AssignWeight();
edges.insert({getWeight, j, j + n}); // Edge between current cell and bottom cell
}
}
}
for (auto [w, u, v] : edges)
{
if (dsu.find(u) != dsu.find(v)) // unite if u and v dont belong to same component
{
dsu.unite(u, v);
mst.insert({u, v});
}
}
for (auto [u, v] : mst) // Removing edges that are present in MST to generate the maze
{
int i = (u - 1) / n, j = (u - 1) % n;
if (v == u + 1) maze[2 * i + 1][2 * j + 2] = ' ';
else maze[2 * i + 2][2 * j + 1] = ' ';
}
start = rand() % n;
end = rand() % n;
maze[0][2 * start + 1] = ' ';
maze[2 * n][2 * (end) + 1] = ' ';
}
int Maze::AssignWeight() // Generates Random Weight
{
return rand()%1000;
}
int dijkstra(Maze m)
{
int n = 2 * m.n + 1;
pair<int, int> start = {0, 2 * m.start + 1}, end = {2 * m.n, 2 * m.end + 1};
vector<vector<int>> dis(n, vector<int>(n, INT_MAX)); // stores min distance from start to the current cell
vector<vector<pair<int, int>>> par(n, vector<pair<int, int>>(n)); // storing the parent cell of each cell , used for backtracking the solution
set<pair<int, pair<int, int>>> s; // stores {min distance to reach the cell , cell}
s.insert({0, start});
while (!s.empty())
{
pair<int, pair<int, int>> a = *s.begin();
s.erase(s.begin());
int dist = a.first, x = a.second.first, y = a.second.second;
if (x < n - 1 and m.maze[x + 1][y] == ' ' and dis[x + 1][y] > dist + 1) // checking if distance to cell can be minimised
{
if (s.find({dis[x + 1][y], {x + 1, y}}) != s.end())
s.erase(s.find({dis[x + 1][y], {x + 1, y}}));
s.insert({dist + 1, {x + 1, y}});
dis[x + 1][y] = dist + 1; // updating the min distance as min distance of parent + 1
par[x + 1][y] = {x, y};
}
if (x > 0 and m.maze[x - 1][y] == ' ' and dis[x - 1][y] > dist + 1) // checking if distance to cell can be minimised
{
if (s.find({dis[x - 1][y], {x - 1, y}}) != s.end())
s.erase(s.find({dis[x - 1][y], {x - 1, y}}));
s.insert({dist + 1, {x - 1, y}});
dis[x - 1][y] = dist + 1; // updating the min distance as min distance of parent + 1
par[x - 1][y] = {x, y};
}
if (y > 0 and m.maze[x][y - 1] == ' ' and dis[x][y - 1] > dist + 1) // checking if distance to cell can be minimised
{
if (s.find({dis[x][y - 1], {x, y - 1}}) != s.end())
s.erase(s.find({dis[x][y - 1], {x, y - 1}}));
s.insert({dist + 1, {x, y - 1}});
dis[x][y - 1] = dist + 1; // updating the min distance as min distance of parent + 1
par[x][y - 1] = {x, y};
}
if (y < n - 1 and m.maze[x][y + 1] == ' ' and dis[x][y + 1] > dist + 1) // checking if distance to cell can be minimised
{
if (s.find({dis[x][y + 1], {x, y + 1}}) != s.end())
s.erase(s.find({dis[x][y + 1], {x, y + 1}}));
s.insert({dist + 1, {x, y + 1}});
dis[x][y + 1] = dist + 1; // updating the min distance as min distance of parent + 1
par[x][y + 1] = {x, y};
}
}
if (dis[end.first][end.second] > 1e9) // Solution will always exist since MST always forms a connected component including all cells
{
cout << "No Solution\n";
return -1;
}
else
{
pair<int, int> x = end;
while (x != start) // Backtracking the Maze Solution
{
int a = x.first, b = x.second;
m.maze[a][b] = '\'';
x = par[a][b];
if (x == start)
break;
}
m.maze[x.first][x.second] = '\'';
m.printmaze();
}
return 0;
}
int main()
{
cout << "Enter the size of the maze : ";
srand(time(0));
int n;
cin >> n;
cout << "\n\n";
Maze m(n);
cout << "Building the Maze........\n\n";
m.buildmaze();
m.printmaze();
cout << "Solution to the Maze using Dijkstra's Algorithm: \n\n";
dijkstra(m);
}