Advertisement
Josif_tepe

Untitled

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