imreally_john

Untitled

Apr 6th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. #define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  5. #define pb push_back
  6.  
  7. int main(){
  8.     FAST_IO
  9.     ll t;
  10.     cin>>t;
  11.  
  12.     while(t--){
  13.  
  14.         ll m,n;
  15.         cin>>m>>n;
  16.  
  17.         vector<pair<ll,ll> > position;
  18.         char ch;
  19.         bool start = true;
  20.         ll curr_i = -1,curr_j = -1;
  21.         ll last_i = -1;
  22.         for(ll i=1;i<=m;i++){
  23.             bool first = true;
  24.             ll left_j=-1, right_j=-1;
  25.             for(ll j=1;j<=n;j++){
  26.                 cin>>ch;
  27.                 if(ch=='P'){
  28.                     if(first)left_j=j;
  29.                     right_j = j;
  30.                     first=false;
  31.                     last_i = i;
  32.                  }
  33.             }
  34.             position.pb({left_j,right_j});
  35.             if(start and left_j!=-1){
  36.                 start = false;
  37.                 curr_i = i;
  38.                 if(i%2==0){
  39.                     curr_j=right_j;
  40.                 }
  41.                 else{
  42.                     curr_j=left_j;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         ll steps = 0;
  48.  
  49.         if(curr_i==-1 and curr_j==-1){
  50.             cout<<steps<<endl;
  51.         }
  52.         else{
  53.             for(ll i=curr_i;i<=last_i;i++){
  54. //                cout<<i<<" "<<curr_j<<" "<<steps<<endl;
  55.                 if(i%2!=0){
  56.                     if(position[i-1].first!=-1){
  57.                         steps += position[i-1].second - curr_j;
  58.                         curr_j = position[i-1].second;
  59.                     }
  60.                     if(i<=m-1){
  61.                         if(position[i].first!=-1){
  62.                             if(position[i].second>curr_j){
  63.                                 steps += position[i].second - curr_j;
  64.                                 curr_j = position[i].second;
  65.                             }
  66.                         }
  67.                     }
  68.                     steps+=1; ///going to next level
  69.                 }
  70.                 else{
  71.                     if(position[i-1].first!=-1){
  72.                         steps += curr_j - position[i-1].first;
  73.                         curr_j = position[i-1].first;
  74.                     }
  75.  
  76.                     if(i<=m-1){
  77.                         if(position[i].first!=-1){
  78.                             if(position[i].first<curr_j){
  79.                                 steps+=curr_j - position[i].first;
  80.                                 curr_j = position[i].first;
  81.                             }
  82.                         }
  83.                     }
  84.                     steps+=1; ///going to next level
  85.                 }
  86.             }
  87.             steps-=1;
  88.             cout<<steps<<endl;
  89.         }
  90.     }
  91. }
Add Comment
Please, Sign In to add comment