YEZAELP

CUBE-122: COI Board Game !

Jun 8th, 2020 (edited)
132
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.  
  3. using namespace std;
  4. const int INF = 1e9;
  5. const int N = 5e2 + 10;
  6. const int M = 500 + 10;
  7. struct Data{
  8.     int dis, i, j, J, B, P;
  9. };
  10. int n, m;
  11. char ar[N][M];
  12. int Dis[N][M][2][2][2];
  13. bool vs[N][M][2][2][2];
  14. int di[] = {0, 0, 1, -1};
  15. int dj[] = {1, -1, 0, 0};
  16.  
  17. bool pst(int i, int j){
  18.     return i >= 1 and i <= n and j >= 1 and j <= m;
  19. }
  20.  
  21. bool check(int i, int j, int J, int B, int P){
  22.     if(ar[i][j] == '.'
  23.     or ar[i][j] == 'j'
  24.     or ar[i][j] == 'b'
  25.     or ar[i][j] == 'p'
  26.     or ar[i][j] == 'S'
  27.     or ar[i][j] == 'E')
  28.         return true;
  29.     if(ar[i][j] == 'J' and J) return true;
  30.     else if(ar[i][j] == 'B' and B) return true;
  31.     else if(ar[i][j] == 'P' and P) return true;
  32.     return false;
  33. }
  34.  
  35. int main(){
  36.  
  37.     scanf("%d%d", &n, &m);
  38.  
  39.     int si = -1, sj = -1, ei = -1, ej = -1;
  40.     for(int i=1;i<=n;i++){
  41.         for(int j=1;j<=m;j++){
  42.             scanf(" %c", &ar[i][j]);
  43.             if(ar[i][j] == 'S'){
  44.                 si = i;
  45.                 sj = j;
  46.             }
  47.             else if(ar[i][j] == 'E'){
  48.                 ei = i;
  49.                 ej = j;
  50.             }
  51.         }
  52.     }
  53.  
  54.     for(int i=1;i<=n;i++){
  55.         for(int j=1;j<=m;j++){
  56.             for(int J=0;J<=1;J++){
  57.                 for(int B=0;B<=1;B++){
  58.                     for(int P=0;P<=1;P++){
  59.                         Dis[i][j][J][B][P] = INF;
  60.                         vs[i][j][J][B][P] = false;
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     queue <Data> q;
  68.     Dis[si][sj][0][0][0] = 0;
  69.     q.push({Dis[si][sj][0][0][0], si, sj, 0, 0, 0});
  70.  
  71.     while(!q.empty()){
  72.         int d = q.front().dis;
  73.         int ui = q.front().i;
  74.         int uj = q.front().j;
  75.         int j = q.front().J;
  76.         int b = q.front().B;
  77.         int p = q.front().P;
  78.         q.pop();
  79.  
  80.         if(vs[ui][uj][j][b][p])
  81.             continue;
  82.         vs[ui][uj][j][b][p] = true;
  83.  
  84.         if(ui == ei and uj == ej){
  85.             printf("%d", d);
  86.             return 0;
  87.         }
  88.  
  89.         for(int dij=0;dij<4;dij++){
  90.             int vi = ui + di[dij];
  91.             int vj = uj + dj[dij];
  92.             if(!pst(vi, vj)) continue;
  93.  
  94.             int jj = j, bb = b, pp = p;
  95.             if(ar[vi][vj] == 'j') jj = 1;
  96.             else if(ar[vi][vj] == 'b') bb = 1;
  97.             else if(ar[vi][vj] == 'p') pp = 1;
  98.  
  99.             if(check(vi, vj, jj, bb, pp) and !vs[vi][vj][jj][bb][pp] and d + 1 < Dis[vi][vj][jj][bb][pp]){
  100.                 Dis[vi][vj][jj][bb][pp] = d + 1;
  101.                 q.push({Dis[vi][vj][jj][bb][pp], vi, vj, jj, bb, pp});
  102.             }
  103.         }
  104.  
  105.     }
  106.  
  107.     printf("-1");
  108.  
  109.     return 0;
  110. }
  111.  
  112. /*
  113.  
  114. 5 5
  115. j...p
  116. .....
  117. .S#P.
  118. .#EB.
  119. PbJ..
  120.  
  121. */
Add Comment
Please, Sign In to add comment