Code:
#include <iostream>
#include <fstream>
using namespace std;
/* ============================= */
/* ===== Global Variables ===== */
/* ============================= */
enum gameStatus {pending, success, impossible};
/* =============================== */
/* ===== Function Prototypes ===== */
/* =============================== */
void ReadMaze(char Maze[10][10],int& row,int& col,int& mouse,int& cheese);
void DrawMaze(char Maze[10][10],int& row,int& col,int& mouse,int& cheese);
void EscapeMaze(char Maze[10][10], int cpx, int cpy, const int spx, const int spy, const int epx, const int epy,
const int hbx, const int hby, gameStatus& status,int& nosol,int row,int col,int cheese,int mouse);
/* ========================= */
/* ===== Main Function ===== */
/* ========================= */
int main()
{
system("cls");
// Defining maze array and positioning variables
int row,col,mouse,cheese;
char Maze[10][10];
ReadMaze(Maze,row,col,mouse,cheese);
cout<<row<<col<<mouse<<cheese<<endl;
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
cout<<Maze[row][col]<<endl;
}
}
int spx = mouse;
int spy = col-1;
int epy = 0;
int epx = 0;
int hbx = 0;
int hby = 0;
int cpx = spx;
int cpy = spy;
int nosol = 0;
// Defining status variable and setting its initial value
gameStatus status;
status = pending;
EscapeMaze(Maze, cpx, cpy, spx, spy, epx, epy, hbx, by,status,nosol,row,col,cheese,mouse);
cout<<"solutions:"<<nosol<<endl;
if (status == impossible)
cout << "No path exists" << endl;
else
DrawMaze(Maze,row,col,mouse,cheese);
system("pause");
return 0;
} /* end main */
/* ================================ */
/* ===== Function Definitions ===== */
/* ================================ */
void ReadMaze(char Maze[10][10],int& row,int& col,int& mouse,int& cheese)
{
int m = -1,n = 0;
ifstream ifile;
ifile.open("input.txt");
ifile>>row;
ifile>>col;
ifile>>mouse;
ifile>>cheese;
while(!ifile.eof())
{
m++;
for (int n = 0; n < col ; n++)
{
ifile >> Maze[m][n];
} /* end for */
} /* end while */
ifile.close();
// cout<<i<<" "<<j<<" "<<k<<" "<<l<<endl;
} /* end ReadMaze */
void DrawMaze(char Maze[10][10],int& row,int& col,int& mouse,int& cheese)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << Maze[i][j] << " ";
} /* end for */
cout << endl;
} /* end for */
} /* end DrawMaze */
void EscapeMaze(char Maze[10][10], int cpx, int cpy, const int spx, const int spy, const int epx, const int epy,
const int hbx, const int hby, gameStatus& status,int& nosol,int row,int col,int cheese,int mouse)
{
DrawMaze(Maze,row,col,mouse,cheese);
cout<<endl;
// system("pause");
if (cpx <= row && cpy <= col && cpx >= hbx && cpy >= hby)
{
if (cpx == cheese && cpy == cheese)
{
Maze[cheese][cheese] = '*';
status = success;
nosol++;
} /* end if */
else
{
Maze[cpy][cpx] = '*';
// Check positions for all directions
if ((Maze[cpy][cpx+1] == '0') || (Maze[cpy][cpx+1] == 'X'))
EscapeMaze(Maze, cpx+1, cpy, spx, spy, epx, epy, hbx, hby, status, nosol, row, col, cheese,mouse); // right
if ((Maze[cpy][cpx+2] == '0') || (Maze[cpy][cpx+2] == 'X'))
EscapeMaze(Maze, cpx+2, cpy, spx, spy, epx, epy, hbx, hby, status, nosol, row, col, cheese,mouse); // right
if ((Maze[cpy][cpx-1] == '0') || (Maze[cpy][cpx-1] == 'X'))
EscapeMaze(Maze, cpx-1, cpy, spx, spy, epx, epy, hbx, hby, status, nosol,row, col, cheese,mouse); // left
if ((Maze[cpy][cpx-2] == '0') || (Maze[cpy][cpx-2] == 'X'))
EscapeMaze(Maze, cpx-2, cpy, spx, spy, epx, epy, hbx, hby, status, nosol,row, col, cheese,mouse); // left
if ((Maze[cpy-1][cpx] == '0') || (Maze[cpy-1][cpx] == 'X'))
EscapeMaze(Maze, cpx, cpy-1, spx, spy, epx, epy, hbx, hby, status, nosol, row, col, cheese,mouse); // up
if ((Maze[cpy-2][cpx] == '0') || (Maze[cpy-2][cpx] == 'X'))
EscapeMaze(Maze, cpx, cpy-2, spx, spy, epx, epy, hbx, hby, status, nosol, row, col, cheese,mouse); // up
Maze[cpy][cpx] = '0'; //I think here it should "open" again..
if (status != success)
{
if (cpx == spx && cpy == spy)
status = impossible;
Maze[cpy][cpx] = '1';
} /* end if */
} /* end else */
} /* end if */
} /* end EscapeMaze */