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
|
bool SolveMaze(char maze[][81], int row, int col, int i, int j)
{
if(i > row || i < 0 || j > col || j < 0) // are we outside the maze?
{
return false;
}
if( i == row - 1 && j == col - 1 ) // are we at the end?
{
return true;
}
if( maze[i][j] == "*" || maze[i][j] == "@" || maze[i][j] == "#" ) // is it open?
{
return false;
}
maze[i][j] = "#"; // mark current location
if(SolveMaze(maze, row, col, i - 1, j) == true) // can I go up?
{
return true;
}
if(SolveMaze(maze, row, col, i, j + 1) == true) // can I go right?
{
return true;
}
if(SolveMaze(maze, row, col, i + 1, j) == true) // can I go down?
{
return true;
}
if(SolveMaze(maze, row, col, i, j - 1) == true) // can I go left?
{
return true;
}
maze[i][j] = "@";
return false;
}
int main(int argc, char *argv[])
{
char maze[24][81];
int row, col;
int i = 0, j = 0;
if(SolveMaze(maze, row, col, i, j))
{
PrintMaze(maze, row, col, cout);
}
else
{
cout << "Didn't work." << endl;
}
return 0;
}
|