Advertisement
MAGCARI

COI board game

Feb 4th, 2023 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 06 February 2023 [21:42]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int i,j,mask;
  11. };
  12. queue<A > que;
  13. char a[510][510];
  14. int dis[510][510][10];
  15. int dir[2][4] = {{-1,1,0,0},{0,0,-1,1}};
  16. // J = 0
  17. // B = 1
  18. // P = 2
  19. int main(){
  20.     cin.tie(0)->sync_with_stdio(0);
  21.     cin.exceptions(cin.failbit);
  22.     int n,m,sti,stj;
  23.     cin >> n >> m;
  24.     for(int i=1;i<=n;i++){
  25.         for(int j=1;j<=m;j++){
  26.             cin >> a[i][j];
  27.             if(a[i][j] == 'S')
  28.                 sti = i,stj = j;
  29.         }
  30.     }
  31.  
  32.     for(int i=1;i<=n;i++)
  33.         for(int j=1;j<=m;j++)
  34.             for(int k=0;k<8;k++)
  35.                 dis[i][j][k] = 1e9;
  36.     dis[sti][stj][0] = 0;
  37.     que.push({sti,stj,0});
  38.     while(!que.empty()){
  39.         A now = que.front();
  40.         que.pop();
  41.         if(a[now.i][now.j] == 'E'){
  42.             cout << dis[now.i][now.j][now.mask];
  43.             return 0;
  44.         }
  45.         for(int k=0;k<4;k++){
  46.             int ni = now.i+dir[0][k],nj = now.j+dir[1][k];
  47.             if(ni<1 || nj<1 || ni>n || nj>m)    continue;
  48.             if(a[ni][nj] == '#')                continue;
  49.             if(a[ni][nj] == 'J' && (now.mask & (1<<0)) == 0)    continue;
  50.             if(a[ni][nj] == 'B' && (now.mask & (1<<1)) == 0)    continue;
  51.             if(a[ni][nj] == 'P' && (now.mask & (1<<2)) == 0)    continue;
  52.             int nmask = now.mask;
  53.             if(a[ni][nj] == 'j')    nmask |= (1<<0);
  54.             if(a[ni][nj] == 'b')    nmask |= (1<<1);
  55.             if(a[ni][nj] == 'p')    nmask |= (1<<2);
  56.  
  57.             if(dis[ni][nj][nmask] > dis[now.i][now.j][now.mask]+1){
  58.                 dis[ni][nj][nmask] = dis[now.i][now.j][now.mask]+1;
  59.                 que.push({ni,nj,nmask});
  60.             }
  61.         }
  62.     }
  63.     cout << "-1\n";
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement