mickypinata

IPST58: Pacman and Ghost

Jul 17th, 2021 (edited)
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 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> tiii;
  6.  
  7. const int N = 100;
  8.  
  9. pii dir[4] = {pii(0, 1), pii(0, -1), pii(1, 0), pii(-1, 0)};
  10. pii dirP[5] = {pii(0, 0), pii(0, 1), pii(0, -1), pii(1, 0), pii(-1, 0)};
  11. char board[N][N + 1];
  12. int dist[N][N], row, col;
  13. bool visited[N][N];
  14.  
  15. bool isInBoard(int r, int c){
  16.     return r >= 0 && r < row && c >= 0 && c < col;
  17. }
  18.  
  19. int main(){
  20.  
  21.     int Q;
  22.     scanf("%d", &Q);
  23.     while(Q--){
  24.         int nGhost, tr, stR, stC;
  25.         scanf("%d%d%d%d%d%d", &row, &col, &nGhost, &tr, &stR, &stC);
  26.         priority_queue<tiii, vector<tiii>, greater<tiii>> pq;
  27.         for(int i = 0; i < row; ++i){
  28.             for(int j = 0; j < col; ++j){
  29.                 dist[i][j] = 1e9;
  30.                 visited[i][j] = false;
  31.             }
  32.         }
  33.         for(int i = 1; i <= nGhost; ++i){
  34.             int d, r, c;
  35.             scanf("%d%d%d", &d, &r, &c);
  36.             if(d < dist[r][c]){
  37.                 pq.emplace(d, r, c);
  38.                 dist[r][c] = d;
  39.             }
  40.         }
  41.         for(int i = 0; i < row; ++i){
  42.             for(int j = 0; j < col; ++j){
  43.                 scanf(" %c", &board[i][j]);
  44.             }
  45.         }
  46.         while(!pq.empty()){
  47.             int ur = get<1>(pq.top());
  48.             int uc = get<2>(pq.top());
  49.             pq.pop();
  50.             if(visited[ur][uc]){
  51.                 continue;
  52.             }
  53.             visited[ur][uc] = true;
  54.             for(int d = 0; d < 4; ++d){
  55.                 int vr = ur + dir[d].first;
  56.                 int vc = uc + dir[d].second;
  57.                 if(isInBoard(vr, vc) && board[vr][vc] != '#' && !visited[vr][vc] && dist[ur][uc] + 1 < dist[vr][vc]){
  58.                     dist[vr][vc] = dist[ur][uc] + 1;
  59.                     pq.emplace(dist[vr][vc], vr, vc);
  60.                 }
  61.             }
  62.         }
  63.         queue<pii> que;
  64.         if(dist[stR][stC] == 0){
  65.             cout << "NO\n";
  66.             continue;
  67.         }
  68.         que.emplace(stR, stC);
  69.         int t, cnt = 1;
  70.         for(t = 0; t < tr; ++t){
  71.             for(int i = 0; i < row; ++i){
  72.                 for(int j = 0; j < col; ++j){
  73.                     visited[i][j] = false;
  74.                 }
  75.             }
  76.             int newCnt = 0;
  77.             for(int i = 1; i <= cnt && !que.empty(); ++i){
  78.                 int ur = que.front().first;
  79.                 int uc = que.front().second;
  80.                 que.pop();
  81.                 for(int d = 0; d < 5; ++d){
  82.                     int vr = ur + dirP[d].first;
  83.                     int vc = uc + dirP[d].second;
  84.                     if(isInBoard(vr, vc) && board[vr][vc] != '#' && !visited[vr][vc] && t + 1 < dist[vr][vc]){
  85.                         visited[vr][vc] = true;
  86.                         ++newCnt;
  87.                         que.emplace(vr, vc);
  88.                     }
  89.                 }
  90.             }
  91.             cnt = newCnt;
  92.             if(cnt == 0){
  93.                 break;
  94.             }
  95.         }
  96.         if(t == tr){
  97.             cout << "YES\n";
  98.         } else {
  99.             cout << "NO\n";
  100.         }
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
Add Comment
Please, Sign In to add comment