Advertisement
mickypinata

TOI15: Fly

Nov 8th, 2021
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef tuple<int, int, int, int> tpp;
  5.  
  6. const int R = 2000;
  7. const int C = 500;
  8.  
  9. int posL[C + C][R + 2], posR[C + C][R + 2], row, col;
  10. char dirL[R + 1], dirR[R + 1];
  11. bool visited[R + 2][C + 2][C + C];
  12.  
  13. int dirToInt(char c){
  14.     if(c == 'R'){
  15.         return 1;
  16.     }
  17.     return -1;
  18. }
  19. int main(){
  20.  
  21.     scanf("%d%d", &row, &col);
  22.     for(int i = 1; i <= row; ++i){
  23.         char dirLeft, dirRight;
  24.         scanf("%d %c%d %c", &posL[0][i], &dirL[i], &posR[0][i], &dirR[i]);
  25.     }
  26.  
  27.     // Precompute trap state
  28.     posL[0][0] = 0;
  29.     posR[0][0] = col;
  30.     posL[0][row + 1] = 0;
  31.     posR[0][row + 1] = col;
  32.     for(int t = 1; t < col + col; ++t){
  33.         posL[t][0] = 0;
  34.         posR[t][0] = col;
  35.         posL[t][row + 1] = 0;
  36.         posR[t][row + 1] = col;
  37.         for(int i = 1; i <= row; ++i){
  38.             if(posL[t - 1][i] + 1 == posR[t - 1][i] && dirL[i] == 'R' && dirR[i] == 'L'){
  39.                 posL[t][i] = posL[t - 1][i];
  40.                 posR[t][i] = posR[t - 1][i];
  41.                 dirL[i] = 'L';
  42.                 dirR[i] = 'R';
  43.                 continue;
  44.             } else if(posL[t - 1][i] == posR[t - 1][i] && dirL[i] == 'R' && dirR[i] == 'L'){
  45.                 posL[t][i] = posL[t - 1][i] - 1;
  46.                 posR[t][i] = posR[t - 1][i] + 1;
  47.                 dirL[i] = 'L';
  48.                 dirR[i] = 'R';
  49.                 continue;
  50.             }
  51.             if(posL[t - 1][i] == 0 && dirL[i] == 'L'){
  52.                 posL[t][i] = 1;
  53.                 dirL[i] = 'R';
  54.             } else {
  55.                 posL[t][i] = posL[t - 1][i] + dirToInt(dirL[i]);
  56.             }
  57.             if(posR[t - 1][i] == col && dirR[i] == 'R'){
  58.                 posR[t][i] = col - 1;
  59.                 dirR[i] = 'L';
  60.             } else {
  61.                 posR[t][i] = posR[t - 1][i] + dirToInt(dirR[i]);
  62.             }
  63.         }
  64.     }
  65.  
  66.     queue<tpp> que; // tpp(dist, row, col, state)
  67.     for(int i = 1; i < col; ++i){
  68.         visited[0][i][0] = true;
  69.         que.emplace(0, 0, i, 0);
  70.     }
  71.     int mxTime = (row + 1) * (col + col - 1);
  72.     for(int t = 0; t <= mxTime; ++t){
  73.         while(!que.empty() && get<0>(que.front()) == t){
  74.             int d = get<0>(que.front());
  75.             int ur = get<1>(que.front());
  76.             int uc = get<2>(que.front());
  77.             int us = get<3>(que.front());
  78.             que.pop();
  79.             if(uc <= posL[us][ur] || uc >= posR[us][ur]){
  80.                 continue;
  81.             }
  82.             if(ur == row + 1){
  83.                 cout << d;
  84.                 return 0;
  85.             }
  86.             int vs = (us + 1) % (col + col);
  87.             if(!visited[ur + 1][uc][vs]){
  88.                 visited[ur + 1][uc][vs] = true;
  89.                 que.emplace(d + 1, ur + 1, uc, vs);
  90.             }
  91.             if(!visited[ur][uc][vs]){
  92.                 visited[ur][uc][vs] = true;
  93.                 que.emplace(d + 1, ur, uc, vs);
  94.             }
  95.         }
  96.     }
  97.  
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement