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
|
#include <iostream>
using namespace std;
bool isSafe(char maze[][4],int x,int y)
{
if (maze[x][y]!='#' && x>=0 && y>=0 && x<4 && y<4)
{
return true;
}
return false;
}
bool isSolve(char maze[][4],int x,int y)
{
if (x==3 && y==3)
{
return true;
}
maze [x][y]='#';
if (isSafe(maze,x+1,y)==true)// SOUTH
{
isSolve(maze,x+1,y);
}
if (isSafe(maze,x,y-1)==true)// WEST
{
isSolve(maze,x,y-1);
}
if (isSafe(maze,x-1,y)==true)// NORTH
{
isSolve(maze,x-1,y);
}
if (isSafe(maze,x,y+1)==true)// EAST
{
isSolve(maze,x,y+1);
}
return false;
}
int main()
{
char arr[4][4]={
{'0','#','0','0'},
{'0','0','0','#'},
{'0','#','#','0'},
{'0','0','0','0'}
};
cout<<isSolve(arr,0,0);
return 0;
}
|