-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPG3.cpp
178 lines (152 loc) · 4.61 KB
/
PG3.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
//
// AUTHOR: Cole Walgren
// TITLE: Program 3
// DESCRIPTION: Returns the shortest path from the start (top left corner)
// to the finish (bottom right corner) of a maze entered by the user
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include "PG3.h"
#include "Queue.hpp"
using namespace std;
int w, h;
char **Maze;
QObj* coord;
Queue *Q;
int main(int argc, const char * argv[]) {
//Ask user for dimensions
cout << "Enter width of maze: "<<endl;
cin >> h;
cout << "Enter height: "<<endl;
cin >> w;
//create Maze board
Maze = new char*[w];
for(int i = 0; i < w; i++){
Maze[i] = new char[h];
}
// user creates maze
cout << "Create a maze below using the '*' and ' ' characters: " << endl;
GetMaze(w, h);
Solve();
PrintMaze2(Maze);
DisposeMaze();
cout<<endl;
return 0;
}
void GetMaze (int r, int c) {
// create r x c maze that the user enters
h = r;
w = c;
cin.ignore();
char** M = new char * [r];
string line;
for(int i = 0; i < r; i++){
M[i] = new char[c];
getline(cin, line);
for(int j = 0; j < c; j++){
M[i][j] = line[j];
}
}
Maze = M;
}
void DisposeMaze () {
// memory deallocation
for (int i=0; i < h; i++)
delete[] Maze[i];
delete[] Maze;
}
void PrintMaze2 (char **M) {
// prints out maze "M"
cout << "PATH FOUND: " << endl;
for(int i = 0; i < h; i++){
for(int k = 0; k < w; k++){
cout << M[i][k];
}
cout << endl;
}
}
int height () {
return h;
}
int width () {
return w;
}
void Solve () {
// Determines each path X is able to make in the maze and compares to determine which is the shortest one.
// After each step, the maze is added to the queue to determine next route and then the memory is deallocated
int r = 0;
int c = 0;
Maze[r][c] = 'X';
Q = new Queue();
Q->ENQUEUE(new QObj(r, c, Maze));
while (!Q->EMPTY()) {
QObj* currentM = Q->DEQUEUE();
r = currentM->getr();
c = currentM->getc();
Maze = currentM->getM();
if (r + 1 == h && c + 1 == w) {
return;
}
if (r != 0) {
if (Maze[r - 1][c] == ' ') {
char** newMaze = new char* [h];
for (int i = 0; i < h; i++) {
newMaze[i] = new char[w];
for (int j = 0; j < w; j++) {
newMaze[i][j] = Maze[i][j];
}
}
newMaze[r - 1][c] = 'X';
Q->ENQUEUE(new QObj(r - 1, c, newMaze));
delete[] newMaze;
}
}
if (r != h - 1) {
if (Maze[r + 1][c] == ' ') {
char** newMaze = new char*[h];
for (int i = 0; i < h; i++) {
newMaze[i] = new char[w];
for (int j = 0; j < w; j++) {
newMaze[i][j] = Maze[i][j];
}
}
newMaze[r + 1][c] = 'X';
Q->ENQUEUE(new QObj(r + 1, c, newMaze));
delete[] newMaze;
}
}
if (c != 0) {
if (Maze[r][c - 1] == ' ') {
char** newMaze = new char*[h];
for (int i = 0; i < h; i++) {
newMaze[i] = new char[w];
for (int j = 0; j < w; j++) {
newMaze[i][j] = Maze[i][j];
}
}
newMaze[r][c - 1] = 'X';
Q->ENQUEUE(new QObj(r, c - 1, newMaze));
delete[] newMaze;
}
}
if (c != w - 1) {
if (Maze[r][c + 1] == ' ') {
char** newMaze = new char* [h];
for (int i = 0; i < h; i++) {
newMaze[i] = new char[w];
for (int j = 0; j < w; j++) {
newMaze[i][j] = Maze[i][j];
}
}
newMaze[r][c + 1] = 'X';
Q->ENQUEUE(new QObj(r, c + 1, newMaze));
delete[] newMaze;
}
}
delete currentM;
}
delete Q;
DisposeMaze();
}