Guest User

Untitled

a guest
Feb 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. bool ratmaze(char maze[10][10],int solution[10][10],int i,int j,int N,int M)
  4. {
  5.  
  6.   if(maze[N][M] == 'X')
  7.     return false;
  8.   if(i==N && j==M)
  9.   {
  10.     solution[i][j]=1;
  11.     for(int x=0;x<=N;x++)
  12.     {
  13.       for(int y=0;y<=M;y++)
  14.       {
  15.         cout<<solution[x][y]<<" ";
  16.       }
  17.       cout<<endl;
  18.     }
  19.     return true;
  20.   }
  21.   if(i>N || j>M || i<0 || j<0)
  22.   {
  23.     return false;
  24.   }
  25.   if(maze[i][j]=='X')
  26.   {
  27.     return false;
  28.   }
  29.   solution[i][j]=1;
  30.   bool right, down, top, left;
  31.   if(i+1 < N && solution[i+1][j]==0 )
  32.   {
  33.     down=ratmaze(maze,solution,i+1,j,N,M);
  34.   }
  35.   if(j+1 < M && solution[i][j+1]==0  )
  36.   {
  37.     right=ratmaze(maze,solution,i,j+1,N,M);
  38.   }
  39.   if( i-1 > 0 && solution[i-1][j]==0)
  40.   {
  41.     top=ratmaze(maze,solution,i-1,j,N,M);
  42.   }
  43.   if(  j-1 > 0 && solution[i][j-1]==0 )
  44.   {
  45.     left=ratmaze(maze,solution,i,j-1,N,M);
  46.   }
  47.   if(left || right || top || down)
  48.   {
  49.     return true;
  50.   }
  51.   solution[i][j]=0;
  52.   return false;
  53. }
  54. int main()
  55. {
  56.     int N,M;
  57.     cin>>N>>M;
  58.     char maze[10][10];
  59.     for(int i=0;i<N;i++)
  60.     {
  61.       for(int j=0;j<M;j++)
  62.       {
  63.         cin>>maze[i][j];
  64.       }
  65.     }
  66.     int solution[10][10]={0};
  67.  
  68.     bool y=ratmaze(maze,solution,0,0,N-1,M-1);
  69.     if(y==false)
  70.     {
  71.       cout<<"NO PATH FOUND";
  72.     }
  73.     return 0;
  74. }
Add Comment
Please, Sign In to add comment