Josif_tepe

Untitled

Mar 18th, 2026
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn = 105;
  6. const int INF = 2e8;
  7. char mat[maxn][maxn];
  8. const int di[4] = {-1, 1, 0, 0};
  9. const int dj[4] = {0, 0, -1, 1};
  10.  
  11. int n, m;
  12.  
  13. int bfs(pair<int, int> p1, pair<int, int> p2) {
  14.     int si = p1.first, sj = p1.second;
  15.     int ei = p2.first, ej = p2.second;
  16.     queue<int> q;
  17.     q.push(si);
  18.     q.push(sj);
  19.     q.push(0);
  20.    
  21.     vector<vector<bool>> visited(n, vector<bool>(m, false));
  22.     visited[si][sj] = true;
  23.    
  24.     while(!q.empty()) {
  25.         int ci = q.front(); q.pop();
  26.         int cj = q.front(); q.pop();
  27.         int dist = q.front(); q.pop();
  28.        
  29.         if(ci == ei and cj == ej) {
  30.             return dist;
  31.         }
  32.        
  33.         for(int k = 0; k < 4; k++) {
  34.             int ti = ci + di[k];
  35.             int tj = cj + dj[k];
  36.            
  37.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != '#') {
  38.                 visited[ti][tj] = true;
  39.                 q.push(ti);
  40.                 q.push(tj);
  41.                 q.push(dist + 1);
  42.             }
  43.         }
  44.     }
  45.    
  46.     return INF;
  47. }
  48. int main()
  49. {
  50.     ios_base::sync_with_stdio(false);
  51.     cin.tie(NULL);
  52.     cin >> n >> m;
  53.    
  54.     int si = 0, sj = 0, ei = 0, ej = 0;
  55.     vector<pair<int, int>> v;
  56.     for(int i = 0; i < n; i++) {
  57.         for(int j = 0; j < m; j++) {
  58.             cin >> mat[i][j];
  59.            
  60.             if(mat[i][j] == 'D') {
  61.                 v.push_back({i, j});
  62.             }
  63.            
  64.             if(mat[i][j] == 'S') {
  65.                 si = i;
  66.                 sj = j;
  67.             }
  68.             if(mat[i][j] == 'B') {
  69.                 ei = i;
  70.                 ej = j;
  71.             }
  72.         }
  73.     }
  74.    
  75.     int res = INF;
  76.     pair<int, int> S = {si, sj};
  77.     pair<int, int> E = {ei, ej};
  78.     if((int) v.size() == 1) {
  79.         res = bfs(S, v[0]) + bfs(v[0], E);
  80.     }
  81.     else if((int) v.size() == 2) {
  82.         res = bfs(S, v[0]) + bfs(v[0], v[1]) + bfs(v[1], E);
  83.         res = min(res, bfs(S, v[1]) + bfs(v[1], v[0]) + bfs(v[0], E));
  84.     }
  85.     else {
  86.         res = bfs(S, v[0]) + bfs(v[0], v[1]) + bfs(v[1], v[2]) + bfs(v[2], E);
  87.         res = min(res, bfs(S, v[0]) + bfs(v[0], v[2]) + bfs(v[2], v[1]) + bfs(v[1], E));
  88.         res = min(res, bfs(S, v[1]) + bfs(v[1], v[0]) + bfs(v[0], v[2]) + bfs(v[2], E));
  89.         res = min(res, bfs(S, v[1]) + bfs(v[1], v[2]) + bfs(v[2], v[0]) + bfs(v[0], E));
  90.         res = min(res, bfs(S, v[2]) + bfs(v[2], v[0]) + bfs(v[0], v[1]) + bfs(v[1], E));
  91.         res = min(res, bfs(S, v[2]) + bfs(v[2], v[1]) + bfs(v[1], v[0]) + bfs(v[0], E));
  92.     }
  93.     if(res >= INF) res = -1;
  94.     cout << res << endl;
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment