-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
224 lines (210 loc) · 8.03 KB
/
dfs.cpp
File metadata and controls
224 lines (210 loc) · 8.03 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
#include<bits/stdc++.h>
using namespace std;
//Class that holds the node members
class Node
{
public:
int row;
int col;
Node* parent;
};
//Class is created for exploration, and to have the code in a structured way
class Explore
{
public:
//This function simply checks for boundaries in the grid
bool checkBounds(int row, int col, int size_row, int size_col)
{
if(row >= size_row || col >= size_col || row < 0 || col < 0)
{
return false;
}
return true;
}
//This is the function that has the algorithm implemented
pair<Node*, vector<vector<bool>>> DFS(vector<vector<int>> grid, int* start, int* goal)
{
//Grid size is extracted
int size_row = grid.size();
int size_col = grid[0].size();
//A stack to add the elements to be explored
stack<pair<int, int>> explore;
//A stack to add nodes to be explored. This is needed to keep a track of nodes since
//the algorithm is implemented using a grid and not a graph
stack<Node*> node;
//Order of exploration in reverse order
//vector<pair<int, int>> directions {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<pair<int, int>> directions {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
//Flag for checking boundaries
bool isValid{false};
pair<Node*, vector<vector<bool>>> output;
//To maintain the list of explored/visited nodes
vector<vector<bool>> visited(size_row, vector<bool> (size_col));
Node* parent = nullptr;
Node* current = nullptr;
parent = new Node();
pair<int, int> current_coords;
pair<int, int> goal_coords {goal[0], goal[1]};
//Loops are used in case they are required for other inputs where there
//can be multiple start points. They are not required in this case but loops are to
//generalize the code
for(int i = start[0]; i < size_row; i++)
{
for(int j = start[1]; j < size_col; j++)
{
current_coords = {i, j};
//Check for obsracles and if the cell is already visited or not
if(grid[i][j] == 0 && !visited[i][j])
{
explore.push({i, j});
node.push(parent);
visited[i][j] = true;
//Run the iterative steps till goal is reached or the grid is explored
//Here, it means that the stack has to be emptied
while(!explore.empty())
{
pair<int, int> coords = explore.top();
parent = node.top();
//Pop a cell to explore the neighbors
explore.pop();
node.pop();
//Mark as visited
visited[coords.first][coords.second] = true;
//Explore neighbors in all directions
for(pair<int, int> dir : directions)
{
//Get row and col
int row = coords.first + dir.first;
int col = coords.second + dir.second;
//Check if goal is reached
if(row == goal[0] && col == goal[1])
{
//Form the goal node as we need to count it
current = new Node();
current->row = row;
current->col = col;
current->parent = parent;
visited[row][col] = true;
goto here;
}
//Check for bounds
isValid = checkBounds(row, col, size_row, size_col);
//Check for obstacles and if the cell is visited or not
if(isValid && !visited[row][col] && grid[row][col] == 0)
{
//For a new node
current = new Node();
current->parent = parent;
current->row = row;
current->col = col;
//Add in the stack
explore.push({row, col});
node.push(current);
isValid = false;
}
}
}
}
}
}
//Goal not found
return output;
here:
//Goal node is current node
output.first = current;
output.second = visited;
return output;
}
};
int main()
{
//Add the grid here
vector<vector<int>> grid
/*
{{0,0,0,1},
{0,0,0,1},
{0,1,0,0},
{0,0,0,0}};
*/
{{0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,0,0,0,1,0},
{0,0,0,0,0,0,0,0,1,0},
{0,0,0,1,1,1,1,1,1,0},
{0,1,0,1,0,0,0,0,0,0},
{0,1,0,1,0,1,1,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,0,0,1,1,0},
{0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0}};
int start[2] {0, 0};
int goal[2] {0, 9};
//steps is a variable that tells us the number of cells that were traversed
//to reach the goal. This is different from the python code presented
int steps{1};
//Path contains a list of all the cells that are traversed to reach the goal
vector<vector<int>> path;
Node* output = nullptr;
vector<vector<bool>> visited;
//This variable is made since we cannot return more than 1 value at a time
//A pair was made to return the path and the nodes visited
pair<Node*, vector<vector<bool>>> output_pair;
Explore Ex;
int size_row = grid.size();
int size_col = grid[0].size();
//Check if goal is in the grid
if(goal[0] >= size_row || goal[1] >= size_col)
{
cout << "Invalid";
}
//Check if goal is obstacle
else if(grid[goal[0]][goal[1]] == 1)
{
cout << "Goal is obtacle";
}
else
{
output_pair = Ex.DFS(grid, start, goal);
output = output_pair.first;
visited = output_pair.second;
//Check if output is empty
if(output == 0)
{
cout << "Goal not found";
}
else
{
//Loop to extract the path from the list of nodes that was formed
while(output->parent != 0)
{
int row = output->row;
int col = output->col;
vector<int> coord {row, col};
path.push_back(coord);
steps++;
output = output->parent;
}
//Add the start coords as in the path
int row = start[0];
int col = start[1];
vector<int> coord {row, col};
path.push_back(coord);
//Reverse the path since we used push_back
reverse(path.begin(), path.end());
//nodes_visited is the 'steps' in the python code
int nodes_visited{0};
//Extract the number of nodes visited from the list
for(int i = 0; i < visited.size(); i++)
{
for(int j = 0; j < visited[0].size(); j++)
{
if(visited[i][j] == 1)
{
nodes_visited++;
}
}
}
//Add a breakpoint here and debug to get the output
cout << "Done";
}
}
}