mickypinata

TOI10: TOI Raider

Jul 5th, 2021 (edited)
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int, int> pii;
  5. typedef tuple<int, int, int, int> tpp;
  6.  
  7. const int N = 97;
  8. const int M = 100;
  9. const int S = 2520;
  10.  
  11. int dirR[6] = {-1, -1, 0, 0, 1, 1};
  12. int dirCEven[6] = {-1, 0, -1, 1, -1, 0};
  13. int dirCOdd[6] = {1, 0, -1, 1, 1, 0};
  14. int board[N][M], row, col;
  15. bool visited[S][N][M];
  16.  
  17. bool isInBoard(int r, int c){
  18.     return r >= 0 && r < row && c >= 0 && c < col;
  19. }
  20.  
  21. int main(){
  22.  
  23.     scanf("%d%d", &row, &col);
  24.     for(int i = 0; i < row; ++i){
  25.         for(int j = 0; j < col; ++j){
  26.             scanf("%d", &board[i][j]);
  27.         }
  28.     }
  29.  
  30.     queue<tpp> que;
  31.     for(int i = ((row - 1) >> 1) - 1; i <= ((row - 1) >> 1) + 1; ++i){
  32.         if(board[i][0] == 1){
  33.             visited[1][i][0] = true;
  34.             que.emplace(i, 0, 1, 1);
  35.         }
  36.     }
  37.     while(!que.empty()){
  38.         int ur = get<0>(que.front());
  39.         int uc = get<1>(que.front());
  40.         int s = get<2>(que.front());
  41.         int dist = get<3>(que.front());
  42.         que.pop();
  43.         if(ur == ((row - 1) >> 1) && uc == col - 1){
  44.             cout << dist;
  45.             return 0;
  46.         }
  47.         int vs = (s + 1) % S;
  48.         bool isEven = (ur + 1) % 2 == 0;
  49.         for(int d = 0; d < 6; ++d){
  50.             int vr = ur + dirR[d];
  51.             int vc = uc + (isEven ? dirCEven[d] : dirCOdd[d]);
  52.             if(isInBoard(vr, vc) && !visited[vs][vr][vc] && board[vr][vc] != 0 && vs % board[vr][vc] == 0){
  53.                 visited[vs][vr][vc] = true;
  54.                 que.emplace(vr, vc, vs, dist + 1);
  55.             }
  56.         }
  57.     }
  58.     cout << "Impossible";
  59.  
  60.     return 0;
  61. }
  62.  
Add Comment
Please, Sign In to add comment