Guest User

Untitled

a guest
May 30th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define f(i,l,n) for(int i=l;i<n;i++)
  6. #define E "\n"
  7. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  8. #define MAX 1e5
  9. #define MIN -1e5
  10. #define float long double
  11.  
  12.  
  13.  
  14. const int M=1e9+7;
  15.  
  16. int n,m;                
  17. string s[1000];                  
  18. int a[1005][1005],b[1005][1005],visited[1005][1005];   //a for moster reach time for cell {x,y} b for A
  19. char perent[1005][1005];                              // to keep trak of path
  20. pair<int,int> points[]={{1,0},{-1,0},{0,1},{0,-1}};   //  D U R  L
  21.  
  22. bool valid(int x,int y)
  23. {
  24.   if(x>=0 && x<n && y>=0 && y<m) return true;
  25.   else return false;
  26. }
  27.  
  28.  
  29. char per(int dx,int dy)
  30. {
  31.   char c;
  32.   if(dx==1)  c='D';
  33.   else if(dx==-1) c='U';
  34.   else if(dy==1)  c='R';
  35.   else if(dy==-1) c='L';
  36.   return c;
  37. }
  38.  
  39. queue<pair<int,int>> q;
  40.  
  41. void bfs()
  42. {
  43.  
  44.   memset(visited,0,sizeof visited);
  45.  
  46.   while(!q.empty())
  47.   {
  48.     pair<int,int> cur=q.front();
  49.     q.pop();
  50.    
  51.     int X=cur.first,Y=cur.second;
  52.     visited[X][Y]=1;
  53.  
  54.     for(auto it:points)
  55.     {
  56.       int x=X+it.first,y=Y+it.second;
  57.       if(valid(x,y) && visited[x][y]==0 && s[x][y]=='.')
  58.       {
  59.          
  60.              visited[x][y]=1;
  61.              a[x][y]=1+a[X][Y];
  62.              q.push({x,y});
  63.       }
  64.     }
  65.   }
  66.  
  67.  
  68. }
  69.  
  70. void bfs1()
  71. {
  72.  
  73.   memset(visited,0,sizeof visited);
  74.  
  75.  
  76.  
  77.   while(!q.empty())
  78.   {
  79.     pair<int,int> cur=q.front();
  80.     q.pop();
  81.     int X=cur.first,Y=cur.second;
  82.     visited[X][Y]=1;
  83.  
  84.  
  85.      if(X==n-1 || Y==m-1 || X==0 || Y==0) //if found any of boundry point then print path
  86.           {
  87.               cout<<"YES\n";
  88.               string ans="";
  89.  
  90.               int x=X,y=Y;
  91.               while(perent[x][y]!='A')
  92.               {
  93.                 //cout<<x<<" "<<y<<E;
  94.                   ans=perent[x][y]+ans;
  95.                   if(perent[x][y]=='L') y++;
  96.                   else if(perent[x][y]=='R') y--;
  97.                   else if(perent[x][y]=='U') x++;
  98.                   else if(perent[x][y]=='D') x--;
  99.               }
  100.              
  101.               cout<<ans.size()<<E<<ans<<E;
  102.               exit(0);
  103.           }
  104.  
  105.     for(auto it:points)
  106.     {
  107.       int x=X+it.first,y=Y+it.second;
  108.       if(valid(x,y) && visited[x][y]==0 && s[x][y]=='.')
  109.       {
  110.          
  111.             visited[x][y]=1;
  112.             b[x][y]=1+b[X][Y];
  113.             perent[x][y]=per(it.first,it.second);
  114.             if(a[x][y]!=-1 && b[x][y]>=a[x][y]) continue;
  115.             q.push({x,y});
  116.          
  117.       }
  118.     }
  119.  
  120.  
  121.   }
  122.   cout<<"NO\n";
  123.  
  124. }
  125. int32_t main()
  126.  
  127. {
  128.   fast;
  129.   #ifndef ONLINE_JUDGE
  130.   freopen("input.txt","r",stdin);
  131.   freopen("output.txt","w",stdout);
  132.   #endif
  133.  
  134.  
  135.  
  136.  
  137.   cin >> n >> m;
  138.  
  139.   memset(a,-1,sizeof a);
  140.   f(i,0,n) cin >> s[i];
  141.  
  142.   pair<int,int> start;
  143.   f(i,0,n)
  144.   {
  145.     f(j,0,m)
  146.     {
  147.       if(s[i][j]=='A') start={i,j},s[i][j]='.';
  148.       if(s[i][j]=='M') q.push({i,j}),a[i][j]=0;
  149.     }
  150.   }
  151. bfs();  
  152.  
  153.  
  154.  
  155.  
  156. perent[start.first][start.second]='A';
  157.  
  158.  q.push(start);
  159.  
  160.   bfs1();
  161.    return 0;
  162. }
Add Comment
Please, Sign In to add comment