Guest User

Untitled

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