On the bottom of this page I have a mostly written program that will not functio
ID: 3634129 • Letter: O
Question
On the bottom of this page I have a mostly written program that will not function correctly for some odd reason. Below are the general specifications for the program I need help on.
MY PROBLEM:
No matter what, once the file is read in it ALWAYS says that the used text file has no escape route, when in fact it does. I need the correct coding solution, not "hints" that aren't really hints and not even helpful.
Generally the solution to the problem is something like this:
If row=10 & col= 10, then you have successfully escaped.
Otherwise:
Try to escape via the South door (direction order is not relevant)
If not successful; Try to escape via the East door.
If not successful; Try to escape via the West door.
If not successful; Try to escape via the North door.
If you have successfully escaped from this position; then print out the current cell positions on your escape path list.
Watch out for cycles, which will cause end less loops because you repeatedly visit the same cell only to run around in a circle. You should visit each cell only once. Try to escape through all 4 doors; if not successful then this cell is a dead-end, the recursive call terminate for this cell.
When you've found an exit route, print the row/col coordinates of each cell on the route. The path you print will be in order, that is the first entry will be row=1, col=1 and the last entry will be row=10, col=10. (If you can't do it in order, you may do it in reverse order if that makes it easier.)
Don't worry about the 1s along the edges when the program runs: There are a safety wall of sorts and prevent false exits.
Read and Echo print the data file. (ratmaze.txt)
(File contents are as follows:)
0 1 1 1 0 1 0 0 1 1
0 0 0 0 0 0 1 1 1 1
0 1 1 1 1 0 1 0 1 1
0 0 0 0 1 0 0 0 1 1
1 1 1 0 1 1 1 0 0 1
1 1 1 0 1 0 0 1 1 1
1 1 0 0 1 1 1 0 0 1
0 0 0 1 0 0 0 1 1 1
0 1 1 1 0 1 0 0 1 1
0 0 0 0 0 1 1 0 0 0
(You may need to make this into a text file of your own to see how the program runs. This program DOES have a valid exit.)
The program must use Recursion to find an exit path out of the maze. (If need be use another loop. I am sufficient enough with recursion to easily convert it to recurison.)
The program must print out the escape route by row & col of cells used to exit.
Cells that are part of a dead end route, shouldn’t be included on the escape route. Change the value to something other than 0 or 1 (probably 2 or 3) - I am not sure how to implement this kind of change so I left it alone. Hopefully some clever programmer out there knows how to do it right. The Boolean value usage is a must.
If there is no exit out of the maze, the program needs to print a message indicating as such.
Below is the code I have:
(further below is sample output of a successful run program with the given text file data.)
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int maze[11][11];
void findExit(int row, int col, bool& success);
void initializeMaze (ifstream& inf);
void echoMaze();
int main()
{
string fname;
int row, col;
bool success = false;
//print program header
cout << "Please enter maze data file name:" << endl; //prompts user to enter file name. Please use the file provided above to compare with the output given below.
cin >> fname; //accepts user input
ifstream inf;
inf.open(fname.c_str()); //opens user specified file
if(inf.fail()) //prints error message if user file failed to open
{
cout << "Input data file failed to open." << endl;
system("pause");
exit(1);
}
//Here file open OK
initializeMaze(inf);
findExit(1, 1, success);
system("pause");
return 0;
}
void initializeMaze (ifstream& inf)
{
int row, col;
// fill whole maze with ones
for (row = 0; row < 12; row++)
for (col = 0; col < 12; col++)
maze [row][col] = 1;
cout << " Maze after all walls ";
echoMaze();
//now load maze with data
for (row = 1; row < 11; row++)
for (col = 1; col < 11; col++)
inf >> maze [row][col];
cout << " Maze after reading data we have ";
echoMaze();
}
//displays the current maze
void echoMaze()
{
int row, col;
//fill whole maze with ones
for (row = 0; row < 12; row++)
{
for (col = 0; col < 12; col++)
cout << maze [row][col] << " ";
cout << endl;
}
cout << endl;
}
void findExit(int row, int col, bool& success)
{
if (success = false)
{
if((row == 10) && (col == 10))
{
success = true;
cout << "Successful Exit!!" << endl;
cout << endl;
cout << "Escape path in order:" << endl;
//make a FOR loop here
cout << "=======>";
cout << "Exit Route : " << " row = " << row << " col = " << col << endl;
}
else if ((success == false) && (maze[row + 1][col] == 0))
{
findExit(row + 1, col, success);
}
else if ((success == false) && (maze[row][col + 1] == 0))
{
findExit(row, col + 1, success);
}
else if ((success == false) && (maze[row][col - 1] == 0))
{
findExit(row, col - 1, success);
}
else if ((success == false) && (maze[row - 1][col] == 0));
{
findExit(row - 1, col, success);
}
}
}
else (success == false && (maze[row - 1][col] == 1) && (maze[row][col - 1] == 1) && (maze[row][col + 1] == 1) && (maze[row + 1][col] == 1));
{
cout << "There is no escape route from this maze." << endl;
}
}
The sample output for a successful program:
=======> Exit Route: row = 1 col = 1
=======> Exit Route: row = 2 col = 1
=======> Exit Route: row = 3 col = 1
=======> Exit Route: row = 4 col = 1
=======> Exit Route: row = 4 col = 2
=======> Exit Route: row = 4 col = 3
=======> Exit Route: row = 4 col = 4
=======> Exit Route: row = 5 col = 4
=======> Exit Route: row = 6 col = 4
=======> Exit Route: row = 7 col = 4
=======> Exit Route: row = 7 col = 3
=======> Exit Route: row = 8 col = 3
=======> Exit Route: row = 8 col = 2
=======> Exit Route: row = 8 col = 1
=======> Exit Route: row = 9 col = 1
=======> Exit Route: row = 10 col = 1
=======> Exit Route: row = 10 col = 2
=======> Exit Route: row = 10 col = 3
=======> Exit Route: row = 10 col = 4
=======> Exit Route: row = 8 col = 6
=======> Exit Route: row = 9 col = 5
=======> Exit Route: row = 8 col = 5
=======> Exit Route: row = 8 col = 6
=======> Exit Route: row = 8 col = 7
=======> Exit Route: row = 9 col = 7
=======> Exit Route: row = 9 col = 8
=======> Exit Route: row = 10 col = 8
=======> Exit Route: row = 10 col = 9
=======> Exit Route: row = 10 col = 10
Many thanks in advance for the help.
Explanation / Answer
This is a recursion maze which the starting object would move in four direction orderly: 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 #include #include #include #define FALSE 0 #define TRUE 1 #define NROWS 3 #define MCOLS 3 using namespace std; static int steps; // Symbols: // '.' = open // 'S' = start // 'G' = goal // '+' = path char maze[NROWS][MCOLS] = { {'S','.','.'}, {'.','.','.'}, {'.','.','G'} }; void display_maze(void); int find_path(int x, int y); int main() { display_maze(); if ( find_path(0, 0) == TRUE ) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.