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
|
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
class Maze {
public:
int SolveMaze(char **a, int x, int y);
void DisplayMaze(char **a, int row, int col);
};
void Maze::DisplayMaze(char **a, int row, int col)
{
for (int i=0; i<col; i++)
{
for (int j=0; j<row; j++)
{
cout<<a[j][i];
}
cout<<endl;
}
}
int Maze::SolveMaze(char **a, int x, int y)
{
int i, j;
if ( i < 0 || i > y - 1 || j < 0 || j > x - 1 ) return false;
if (a[y][x] != '.')
return false;
//Marks as part of solution
a[y][x]='!';
if(SolveMaze( a,x,y-1)==true) return true;//North
if(SolveMaze( a,x+1,y)==true) return true;//East
if(SolveMaze( a,x,y+1)==true) return true;//South
if(SolveMaze( a,x-1,y)==true) return true;//West
//Marks as not part of the solution
a[y][x]='x';
return false;
}
int main()
{
int row, col;
char paths;
char **maze = new char*[col];
ifstream infile;
infile.open("maze.txt");
if(!infile)
{
cout<<"ERROR"<<endl;
exit(1);
}
infile >> row >> col;
cout<<row<<col<<endl;
//Fills Array from File
for (int i=0; i<col; i++)
{
maze[i]=new char[row];
}
for (int i=0; i<col; i++)
{
for (int j=0; j<row; j++)
{
infile>>paths;
maze[j][i]=paths;
}
}
Maze laby;
laby.DisplayMaze(maze, row, col); //Displays maze
laby.SolveMaze(maze, 0, 0); //Traverse maze
laby.DisplayMaze(maze, row, col);//Displays the new Traverse maze
system("pause");
return 0;
}
|