MAGCARI

Slikar

Nov 7th, 2022 (edited)
914
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 07 November 2022 [20:39]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int i,j,state,type;
  11. };
  12. queue<A > que;
  13. char a[55][55];
  14. int dir[2][4] = {{1,-1,0,0},{0,0,1,-1}};
  15. int main(){
  16.     cin.tie(0)->sync_with_stdio(0);
  17.     cin.exceptions(cin.failbit);
  18.     int r,c,sti,stj,eni,enj;
  19.     cin >> r >> c;
  20.     for(int i=1;i<=r;i++){
  21.         for(int j=1;j<=c;j++){
  22.             cin >> a[i][j];
  23.             if(a[i][j] == 'S')
  24.                 sti = i,stj = j;
  25.             else if(a[i][j] == 'D')
  26.                 eni = i,enj = j;
  27.             else if(a[i][j] == '*')
  28.                 que.push({i,j,0,1});
  29.         }
  30.     }
  31.     que.push({sti,stj,0,0});
  32.     while(!que.empty()){
  33.         A now = que.front();
  34.         que.pop();
  35.         if(now.type == 0 && now.i == eni && now.j == enj){
  36.             cout << now.state << '\n';
  37.             return 0;
  38.         }
  39.         for(int k=0;k<4;k++){
  40.             int ni = now.i + dir[0][k],nj = now.j + dir[1][k];
  41.             if(ni<1 || nj<1 || ni>r || nj>c)            continue;
  42.             if(a[ni][nj] == 'X' || a[ni][nj] == '*')    continue;
  43.             if(now.type == 1 && a[ni][nj] == 'D')       continue;
  44.             a[ni][nj] = '*';
  45.             que.push({ni,nj,now.state+1,now.type});
  46.         }
  47.     }
  48.     cout << "KAKTUS\n";
  49.     return 0;
  50. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment