#include<iostream>
using namespace std ;
const int N=5;
bool ispath( int arr[][N], int x,int y,int n)
{ if (x<n && y< n && arr[x][y]==1)
{
return true;
}
return false;
}
bool pathofmaze(int arr[N][N],int x,int y,int n ,int solarr[][N])
{
if( y==(n)&& x==(n-1) ) //base condition
{
solarr[x][y]==1;
return true;
}
if ( ispath(arr,x,y,n) )
{
solarr[x][y]=1;
if(pathofmaze(arr,x,y+1,n,solarr))
{
return true;
}
if(pathofmaze(arr,x+1,y,n,solarr) )
{
return true;
}
solarr[x][y]=0;// backtracking
return false;
}
return false;
}
int main()
{
int n=5,i,j;
int arr[N][N] ={{1,0,1,0,1},{1,1,1,1,0},{0,1,0,1,0},{1,0,0,1,1},{1,0,1,0,1}};
int solarr[N][N]={0};
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
if(pathofmaze(arr,0,0,5,solarr))
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<solarr[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}