Advertisement
Josif_tepe

Untitled

Sep 7th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 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.     while(!q.empty()) {
  17.         int ci = q.front();
  18.         q.pop();
  19.         int cj = q.front();
  20.         q.pop();
  21.         int dist = q.front();
  22.         q.pop();
  23.        
  24.         if(ci == ei and cj == ej) {
  25.             return dist;
  26.         }
  27.         if(ci + 1 < n and mat[ci + 1][cj] != '#' and !visited[ci + 1][cj]) {
  28.             q.push(ci + 1);
  29.             q.push(cj);
  30.             q.push(dist + 1);
  31.             visited[ci + 1][cj] = true;
  32.         }
  33.         if(ci - 1 >= 0 and mat[ci - 1][cj] != '#' and !visited[ci - 1][cj]) {
  34.             q.push(ci - 1);
  35.             q.push(cj);
  36.             q.push(dist + 1);
  37.             visited[ci - 1][cj] = true;
  38.         }
  39.         if(cj + 1 < m and mat[ci][cj + 1] != '#' and !visited[ci][cj + 1]) {
  40.             q.push(ci);
  41.             q.push(cj + 1);
  42.             q.push(dist + 1);
  43.             visited[ci][cj + 1] = true;
  44.         }
  45.         if(cj - 1 >= 0 and mat[ci][cj - 1] != '#' and !visited[ci][cj - 1]) {
  46.             q.push(ci);
  47.             q.push(cj - 1);
  48.             q.push(dist + 1);
  49.             visited[ci][cj - 1] = true;
  50.         }
  51.        
  52.     }
  53.     return 1e8;
  54. }
  55. int main() {
  56.     cin >> n >> m;
  57.     vector<pair<int, int>> d;
  58.     int drinks = 0;
  59.     int si, sj, ei, ej;
  60.     for(int i = 0; i < n; i++) {
  61.         for(int j = 0; j < m; j++) {
  62.             cin >> mat[i][j];
  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.             if(mat[i][j] == 'D') {
  73.                 d.push_back(make_pair(i, j));
  74.                 drinks++;
  75.             }
  76.         }
  77.     }
  78.     int res = 2e9;
  79.     if(drinks == 1) {
  80.         res = bfs(si, sj, d[0].first, d[0].second) + bfs(d[0].first, d[0].second, ei, ej);
  81.     }
  82.     else if(drinks == 2) {
  83.         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);
  84.         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);
  85.         res = min(r1, r2);
  86.     }
  87.     else {
  88.         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);
  89.         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);
  90.         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);
  91.         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);
  92.         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);
  93.         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);
  94.         res = min(min(r1, r2), min(r3, r4));
  95.         res = min(res, min(r5, r6));
  96.  
  97.     }
  98.     if(res > 1e8) {
  99.         res = -1;
  100.     }
  101.     cout << res << endl;
  102.     return 0;
  103. }
  104.  
  105. /*
  106. 5 6
  107. #S.###
  108. #...##
  109. .#..##
  110. D..B#.
  111. ##.#..
  112.  
  113.  **/
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement