-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_visualizer.cpp
211 lines (179 loc) · 4.04 KB
/
graph_visualizer.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
209
210
211
#include<bits/stdc++.h>
#include <windows.h>
#define max 10
using namespace std;
struct point{
int x, y;
};
void printOriginalMatrix(vector<vector<int>> matrix)
{
for(int i=0;i<max;i++)
{
cout << " | ";
for(int j=0;j<max - 1;j++)
{
cout << matrix[i][j] << " || ";
}
cout << matrix[i][max-1] << " | ";
cout << endl;
}
}
void printBooleanMatrix(vector<vector<bool>>&matrix)
{
for(int i=0;i<max;i++)
{
cout << " | ";
for(int j=0;j<max - 1;j++)
{
if(matrix[i][j])
cout << "1" << " || ";
else
cout << "0" << " || ";
}
if(matrix[i][max-1])
cout << "1" << " || ";
else
cout << "0" << " || ";
cout << endl;
}
}
void printPath(vector<point> path)
{
vector<vector<bool>> pathmap(max, vector<bool>(max, false));
for(int i=0;i<path.size();i++)
{
int x1 = path[i].x;
int y1 = path[i].y;
pathmap[x1][y1] = true;
}
cout << "\n\n\n";
printBooleanMatrix(pathmap);
}
bool isSafe(vector<vector<int>> &matrix, vector<vector<bool>> vis, int x, int y)
{
if(x<0 || x>=max || y<0 || y>=max || vis[x][y] == true || matrix[x][y] == -1)
return false;
return true;
}
void bfs(vector<vector<int>> matrix, vector<vector<bool>> vis, point start)
{
queue<point> q;
q.push(start);
vis[start.x][start.y] = true;
bool found = false;
while(!q.empty())
{
point temp = q.front(); q.pop();
vis[temp.x][temp.y] = true;
if(matrix[temp.x][temp.y] == 2)
{
found = true;
system ("CLS");
printBooleanMatrix(vis);
Sleep(20);
break;
}
system ("CLS");
printBooleanMatrix(vis);
Sleep(20);
point temp2;
if(isSafe(matrix, vis, temp.x + 1, temp.y))
{
temp2.x = temp.x + 1;
temp2.y = temp.y;
q.push(temp2);
}
if(isSafe(matrix, vis, temp.x - 1, temp.y))
{
temp2.x = temp.x - 1;
temp2.y = temp.y;
q.push(temp2);
}
if(isSafe(matrix, vis, temp.x, temp.y + 1))
{
temp2.x = temp.x;
temp2.y = temp.y + 1;
q.push(temp2);
}
if(isSafe(matrix, vis, temp.x, temp.y - 1))
{
temp2.x = temp.x;
temp2.y = temp.y - 1;
q.push(temp2);
}
}
if(found)
cout << "A way was found" << endl;
}
void dfs(vector<vector<int>> &matrix, vector<vector<bool>> &vis, point start, bool &flag, vector<point> path)
{
if(start.x < 0 || start.x >= max || start.y < 0 || start.y >=max || vis[start.x][start.y] == true || matrix[start.x][start.y] == -1 || flag == true)
return;
vis[start.x][start.y] = true;
path.push_back(start);
if(matrix[start.x][start.y] == 2)
{
vis[start.x][start.y] = true;
flag = true;
cout << "A way was found!!" << endl;
printPath(path);
return;
}
system ("CLS");
printBooleanMatrix(vis);
Sleep(20);
point temp;
temp.x = start.x + 1;
temp.y = start.y;
dfs(matrix, vis, temp, flag, path);
temp.x = start.x - 1;
temp.y = start.y;
dfs(matrix, vis, temp, flag, path);
temp.x = start.x;
temp.y = start.y + 1;
dfs(matrix, vis, temp, flag, path);
temp.x = start.x;
temp.y = start.y - 1;
dfs(matrix, vis, temp, flag, path);
path.pop_back();
}
int main(void)
{
vector<vector<int>> matrix(max, vector<int>(max, 0));
cout << "Enter the intial point-:\t";
point start;
point destination;
cin >> start.x >> start.y;
cout << "Enter the destination point-: \t";
cin >> destination.x >> destination.y;
matrix[start.x][start.y] = 1;
matrix[destination.x][destination.y] = 2;
cout << "Enter the walls in a place-: \t";
int no_of_walls;
cin >> no_of_walls;
for(int i=0;i<no_of_walls;i++)
{
point temp;
cin >> temp.x >> temp.y;
matrix[temp.x][temp.y] = -1;
}
vector<vector<bool>> vis(max, vector<bool>(max, false));
cout << "Enter the search method \n 1) BFS \n 2) DFS ";
int method;
cin >> method;
if(method == 1)
{
bfs(matrix, vis, start);
}
else if(method == 2)
{
bool flag = false;
vector<point> path;
dfs(matrix, vis, start, flag, path);
}
else
{
cout << "Pressing the wrong number eh...;";
}
return 0;
}