Naxocist

Slikar

Apr 28th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INF 1e9
  5.  
  6. using ll = long long;
  7. using pi = pair<int, int>;
  8. using pii = pair<pi, int>;
  9.  
  10. const int N = 53;
  11. int tr[] = {-1, 1, 0, 0}, tc[] = {0, 0, -1, 1};
  12. char mp[N][N];
  13.  
  14. int main(){
  15.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  16.     int n, m; cin >> n >> m;
  17.  
  18.     pi st, tar;
  19.     queue<pii> q;
  20.     for(int i=0; i<n; ++i){
  21.         for(int j=0; j<m; ++j){
  22.             char &c = mp[i][j];
  23.             cin >> c;
  24.             if(c == 'S'){
  25.                 st = {i, j};
  26.             }
  27.             if(c == 'D'){
  28.                 tar = {i, j};
  29.             }
  30.             if(c == '*'){
  31.                 q.push({{i, j}, 0});
  32.             }
  33.  
  34.         }
  35.     }
  36.    
  37.     q.push({{st.first, st.second}, 0});
  38.    
  39.     while(q.size()){
  40.         pii t = q.front(); q.pop();
  41.         int r = t.first.first, c = t.first.second, u = t.second;
  42.         int type = (mp[r][c] == '*' ? 0 : 1);
  43.        
  44.         if(tar.first == r && tar.second == c){
  45.             cout << u;
  46.             return 0;
  47.         }
  48.  
  49.         for(int i=0; i<4; ++i){
  50.             int h = r+tr[i], k = c+tc[i];
  51.             if(h < 0 || k < 0 || h >= n || k >= m) continue ;
  52.  
  53.             if(type && (mp[h][k] == '.' || mp[h][k] == 'D')){
  54.                 mp[h][k] = 'S';
  55.                 q.push({{h, k}, u+1});
  56.             }
  57.             else if(!type && (mp[h][k] == '.' && mp[h][k] != 'D')){
  58.                 mp[h][k] = '*';
  59.                 q.push({{h, k}, u+1});
  60.             }
  61.         }
  62.     }
  63.  
  64.     cout << "KAKTUS";
  65.     return 0;
  66.  
  67. }
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment