MAGCARI

sol_wa

Jun 12th, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. /*
  2.     TASK: AG_Run Loop Peatt
  3.     LANG: CPP
  4.     AUTHOR: PeaTT~
  5.     SOLUTION: BFS + Greedy
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int di[]={+1,0,+0,-1},dj[]={+0,-1,+1,+0},dist[1010][1010];
  10. char grid[1010][1010];
  11. queue< pair<int, int> > bfs;
  12. void play(){
  13.     int r,c,a,si,sj,ii,jj,i,j;
  14.     cin>>r>>c>>a;
  15.     for(int i=0;i<r;i++){
  16.         for(int j=0;j<c;j++){
  17.             cin>>grid[i][j];
  18.             if(grid[i][j]=='P')
  19.                 si=i,sj=j;
  20.         }
  21.     }
  22.     if(a%2==1){
  23.         cout<<"Can\'t do"<<endl;
  24.         return ;
  25.     }
  26.     bfs.push({si,sj});
  27.     memset(dist,-1,sizeof dist);
  28.     dist[si][sj] = 0;
  29.     while( !bfs.empty() ){      //BFS find dist from P
  30.         i = bfs.front().first, j= bfs.front().second;
  31.         bfs.pop();
  32.         for(int k=0;k<4;k++){
  33.             ii = i+di[k], jj=j+dj[k];
  34.             if(ii<0||jj<0||ii>=r||jj>=c)    continue;
  35.             if(grid[ii][jj]=='#')           continue;
  36.             if(dist[ii][jj]!=-1)            continue;
  37.             dist[ii][jj] = dist[i][j] + 1;
  38.             bfs.push({ii,jj});
  39.         }
  40.     }
  41.     int left = a/2;
  42.     vector<int> ans;
  43.     i = si, j = sj;
  44.     while(left--){
  45.         for(int k=0;k<4;k++){       //Greedy choose
  46.             ii = i+di[k], jj = j+dj[k];
  47.             if(ii<0||jj<0||ii>=r||jj>=c)    continue;
  48.             if(grid[ii][jj]=='#')           continue;
  49.             if(dist[ii][jj]>a/2)            continue; //Checking for valid move
  50.             ans.push_back(k);
  51.             i = ii, j= jj;
  52.             break;
  53.         }
  54.     }
  55.     if(ans.size()==a/2){
  56.         for(auto x: ans){
  57.             if(x==0)    cout<<"D";
  58.             else if(x==1)   cout<<"L";
  59.             else if(x==2)   cout<<"R";
  60.             else if(x==3)   cout<<"U";
  61.         }
  62.         reverse(ans.begin(),ans.end());
  63.         for(auto x: ans){
  64.             if(x==3)    cout<<"D";
  65.             else if(x==2)   cout<<"L";
  66.             else if(x==1)   cout<<"R";
  67.             else if(x==0)   cout<<"U";
  68.         }
  69.         cout<<endl;
  70.     }
  71.     else
  72.         cout<<"Can\'t do"<<endl;
  73. }
  74. int main(){
  75.     ios_base::sync_with_stdio(NULL);
  76.     cin.tie(NULL);
  77.     cout.tie(NULL);
  78.     int q;
  79.     cin >> q;
  80.     while(q--)
  81.         play();
  82.     return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment