R
Roger Douglass
I've been working on this program for school and I just about got it before
I had to turn it in.
Here's my final code on it. I didn't finish
it, but I think I'm pretty close. M is the starting point in the maze and W
the finish. Using recursion the program is to leave a trail of asterisks
(*) showing the solution.
Well, I got stumped on this one. I found something similar to it online,
but not quite what I was looking for.
Here's the code. Maybe someone out there can improve on this.
Roger
P.S. This compiles okay, it just doesn't do what it's supposed to do.
#include <iostream>
using namespace std;
int col = 1;
int row = 2;
const ROWMAX = 11;
const COLMAX = 16;
char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B','B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B','B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'}
};
void printMaze(char[]);
void runMaze(char *, int, int);
int main()
{
char * pt = new char;
pt = &maze[ROWMAX + 1][COLMAX + 1];
cout << "Maze before solution:\n";
printMaze(pt);
cout << "Maze after solution:\n";
runMaze(pt, 1, 2);
delete pt;
return 0;
}
void printMaze(char[])
{
for(int row = 1; row <= ROWMAX; row++)
{
for(int col=1; col <= COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void runMaze(char * ptmaze, int row, int col)
{
if(1 <= row && row <= ROWMAX && 1 <= col && col <= COLMAX)
{
if(' ' == maze[row][col])
{
maze[row][col] = '*';
if(row == 1 || row == ROWMAX || col == 1 || col == COLMAX)
printMaze(ptmaze);
else
{
runMaze(ptmaze, row - 1, col);
runMaze(ptmaze, row, col + 1);
runMaze(ptmaze, row + 1, col);
runMaze(ptmaze, row, col - 1);
}
}
}
}
I had to turn it in.
Here's my final code on it. I didn't finish
it, but I think I'm pretty close. M is the starting point in the maze and W
the finish. Using recursion the program is to leave a trail of asterisks
(*) showing the solution.
Well, I got stumped on this one. I found something similar to it online,
but not quite what I was looking for.
Here's the code. Maybe someone out there can improve on this.
Roger
P.S. This compiles okay, it just doesn't do what it's supposed to do.
#include <iostream>
using namespace std;
int col = 1;
int row = 2;
const ROWMAX = 11;
const COLMAX = 16;
char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B','B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B','B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'}
};
void printMaze(char[]);
void runMaze(char *, int, int);
int main()
{
char * pt = new char;
pt = &maze[ROWMAX + 1][COLMAX + 1];
cout << "Maze before solution:\n";
printMaze(pt);
cout << "Maze after solution:\n";
runMaze(pt, 1, 2);
delete pt;
return 0;
}
void printMaze(char[])
{
for(int row = 1; row <= ROWMAX; row++)
{
for(int col=1; col <= COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void runMaze(char * ptmaze, int row, int col)
{
if(1 <= row && row <= ROWMAX && 1 <= col && col <= COLMAX)
{
if(' ' == maze[row][col])
{
maze[row][col] = '*';
if(row == 1 || row == ROWMAX || col == 1 || col == COLMAX)
printMaze(ptmaze);
else
{
runMaze(ptmaze, row - 1, col);
runMaze(ptmaze, row, col + 1);
runMaze(ptmaze, row + 1, col);
runMaze(ptmaze, row, col - 1);
}
}
}
}