Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- bool ratmaze(char maze[10][10],int solution[10][10],int i,int j,int N,int M)
- {
- if(maze[N][M] == 'X')
- return false;
- if(i==N && j==M)
- {
- solution[i][j]=1;
- for(int x=0;x<=N;x++)
- {
- for(int y=0;y<=M;y++)
- {
- cout<<solution[x][y]<<" ";
- }
- cout<<endl;
- }
- return true;
- }
- if(i>N || j>M || i<0 || j<0)
- {
- return false;
- }
- if(maze[i][j]=='X')
- {
- return false;
- }
- solution[i][j]=1;
- bool right, down, top, left;
- if(i+1 < N && solution[i+1][j]==0 )
- {
- down=ratmaze(maze,solution,i+1,j,N,M);
- }
- if(j+1 < M && solution[i][j+1]==0 )
- {
- right=ratmaze(maze,solution,i,j+1,N,M);
- }
- if( i-1 > 0 && solution[i-1][j]==0)
- {
- top=ratmaze(maze,solution,i-1,j,N,M);
- }
- if( j-1 > 0 && solution[i][j-1]==0 )
- {
- left=ratmaze(maze,solution,i,j-1,N,M);
- }
- if(left || right || top || down)
- {
- return true;
- }
- solution[i][j]=0;
- return false;
- }
- int main()
- {
- int N,M;
- cin>>N>>M;
- char maze[10][10];
- for(int i=0;i<N;i++)
- {
- for(int j=0;j<M;j++)
- {
- cin>>maze[i][j];
- }
- }
- int solution[10][10]={0};
- bool y=ratmaze(maze,solution,0,0,N-1,M-1);
- if(y==false)
- {
- cout<<"NO PATH FOUND";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment