-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
193 lines (143 loc) · 4.24 KB
/
main.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
//
// main.cpp
// maze_solver
//
// Created by Bahadır on 26.10.2017.
// Copyright © 2017 Bahadir. All rights reserved.
//
#include <iostream>
#include <iostream>
#include <string>
#include <vector>
#include "linkedStack.hpp"
using namespace std;
void check_around (vector<vector<int>> &maze, Stack &stack) {
// Current coordinates.
int x = stack.get_x();
int y = stack.get_y();
// First check if we find an exit.
bool is_out = (maze[x+1][y] == 9) || (maze[x-1][y] == 9) || (maze[x][y+1] == 9) || (maze[x][y-1] == 9);
// Check and push the exit coordinate
if (is_out){
cout << "Maze Solved!" << endl;
// add the exit into the stack.
if (maze[x+1][y] == 9)
stack.push(x+1, y);
else if (maze[x-1][y] == 9)
stack.push(x-1, y);
else if (maze[x][y+1] == 9)
stack.push(x, y+1);
else if (maze[x][y-1] == 9)
stack.push(x, y-1);
// Print the stack here...
cout << "Printing the stack: " << endl;
stack.print();
return; // To make sure it is out of recursion
}
// Check north
else if (maze[x-1][y] == 0) {
maze[x][y] = 1;
stack.push(x-1, y);
//cout << "Added: " << x-1 << " " << y << endl;
check_around(maze, stack);
}
// Check east
else if (maze[x][y+1] == 0) {
maze[x][y] = 1;
stack.push(x, y+1);
//cout << "Added: " << x << " " << y+1 << endl;
check_around(maze, stack);
}
// Check south
else if (maze[x+1][y] == 0) {
maze[x][y] = 1;
stack.push(x+1, y);
//cout << "Added: " << x+1 << " " << y << endl;
check_around(maze, stack);
}
// Check west
else if (maze[x][y-1] == 0) {
maze[x][y] = 0;
stack.push(x, y-1);
//cout << "Added: " << x << " " << y-1 << endl;
check_around(maze, stack);
}
// We have a dead end.
// pop the stack and check again for possible ways.
else {
stack.pop();
maze[stack.get_prev_x()][stack.get_prev_y()] = 1;
//cout << "Dead end! Going back!" << endl;
check_around(maze, stack);
}
}
int main() {
/*
Take inputs and construct matrix.
*/
int size_x, size_y;
int start_x, start_y;
cin >> size_x >> size_y;
cin >> start_x >> start_y;
struct mazeNode {
int val;
bool visited;
};
vector<vector<int>> maze(size_x, vector<int>(size_y));
int value;
for (int i = 0; i < size_x; i++) {
for (int j = 0; j < size_y; j++) {
cin.clear();
cin >> value;
maze[i][j] = value;
}
}
// Print matrix ect.
cout << endl;
cout << size_x << " " << size_y << endl;
cout << start_x << " " << start_y << endl;
for (int i = 0; i < size_x; i++) {
for (int j = 0; j < size_y; j++) {
cout << maze[i][j] << " ";
}
cout << endl;
}
// Label start with 2
maze[start_x][start_y] = 2;
// label exits with 9
for (int i = 0; i < size_x; i++) {
if (maze[i][0] == 0)
maze[i][0] = 9;
if (maze[i][size_y-1] == 0)
maze[i][size_y-1] = 9;
}
for (int i = 0; i < size_y; i++) {
if (maze[0][i] == 0)
maze[0][i] = 9;
if (maze[size_x-1][i] == 0)
maze[size_x-1][i] = 9;
}
// Start!
Stack stack(start_x, start_y); // push start index as start index
int x = start_x;
int y = start_y;
// Check around and move.
if (x == 0){
stack.push(x+1, y);
}
else if (x == size_x-1) {
stack.push(x-1, y);
}
else if (y == 0) {
stack.push(x, y+1);
}
else if (y == size_y-1) {
stack.push(x, y-1);
}
//cout << "First step: " << stack.get_x() << " " << stack.get_y() << endl;
// When a coordinate is visited, make its value 1. So be sure that you will never visit it again.
maze[stack.get_x()][stack.get_y()] = 1;
// Start the recursive function.
check_around(maze, stack);
return 0;
}