Advertisement
Josif_tepe

Untitled

Nov 15th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include<ctime>
  3. #include<map>
  4. #include<vector>
  5. #include<queue>
  6. #include <algorithm>
  7. using namespace std;
  8. int n, m;
  9. char mat[105][105];
  10. bool visited[105][105];
  11. int bfs(int si, int sj, int ei, int ej) {
  12.     for(int i = 0; i < n; i++) {
  13.         for(int j = 0; j < m; j++) {
  14.             visited[i][j] = false;
  15.         }
  16.     }
  17.     queue<int> q;
  18.     q.push(si);
  19.     q.push(sj);
  20.     q.push(0);
  21.     visited[si][sj] = true;
  22.     while(!q.empty()) {
  23.         int ci = q.front();
  24.         q.pop();
  25.         int cj = q.front();
  26.         q.pop();
  27.         int dist = q.front();
  28.         q.pop();
  29.        
  30.         if(ci == ei and cj == ej) {
  31.             return dist;
  32.         }
  33.        
  34.         if(ci + 1 < n and !visited[ci + 1][cj] and mat[ci + 1][cj] != '#') {
  35.             q.push(ci + 1);
  36.             q.push(cj);
  37.             q.push(dist + 1);
  38.             visited[ci + 1][cj ] = true;
  39.         }
  40.         if(ci - 1 >= 0 and !visited[ci - 1][cj] and mat[ci - 1][cj] != '#') {
  41.             q.push(ci - 1);
  42.             q.push(cj);
  43.             q.push(dist + 1);
  44.             visited[ci - 1][cj ] = true;
  45.         }
  46.         if(cj + 1 < m and !visited[ci][cj + 1] and mat[ci][cj + 1] != '#') {
  47.             q.push(ci);
  48.             q.push(cj + 1);
  49.             q.push(dist + 1);
  50.             visited[ci][cj + 1] = true;
  51.         }
  52.         if(cj - 1 >= 0 and !visited[ci][cj - 1] and mat[ci][cj - 1] != '#') {
  53.             q.push(ci);
  54.             q.push(cj - 1);
  55.             q.push(dist + 1);
  56.             visited[ci][cj- 1 ] = true;
  57.         }
  58.     }
  59.     return -1;
  60. }
  61. int main()
  62. {
  63.     cin >> n >> m;
  64.     pair<int, int> idx[5];
  65.     int tmp = 1;
  66.     int drinks = 0;
  67.     for(int i = 0; i < n; i++) {
  68.         for(int j = 0; j < m; j++) {
  69.             cin >> mat[i][j];
  70.            
  71.             if(mat[i][j] == 'S'){
  72.                 idx[0] = make_pair(i, j);
  73.             }
  74.             if(mat[i][j] == 'D') {
  75.                 idx[tmp] = make_pair(i, j);
  76.                 drinks++;
  77.                 tmp++;
  78.             }
  79.             if(mat[i][j] == 'B') {
  80.                 idx[4] = make_pair(i, j);
  81.             }
  82.         }
  83.     }
  84.     int perm[drinks + 1];
  85.     for(int i = 0; i < drinks; i++) {
  86.         perm[i] = i + 1;
  87.     }
  88.     int si = idx[0].first;
  89.     int sj = idx[0].second;
  90.    
  91.     int bi = idx[4].first;
  92.     int bj = idx[4].second;
  93.     int result = 2e9;
  94.     do {
  95.         int p = 0;
  96.         for(int i = 0; i < drinks; i++) {
  97.             if(i == 0) {
  98.                 int x = bfs(si, sj, idx[perm[i]].first, idx[perm[i]].second);
  99.                 if(x == -1) {
  100.                     cout << -1 << endl;
  101.                     return 0;
  102.                 }
  103.                 p += x;
  104.             }
  105.             else {
  106.                 int x = bfs(idx[perm[i - 1]].first, idx[perm[i - 1]].second, idx[perm[i]].first, idx[perm[i]].second);
  107.                 if(x == -1) {
  108.                     cout << -1 << endl;
  109.                     return 0;
  110.                 }
  111.                 p += x;
  112.             }
  113.         }
  114.         int x= bfs(idx[perm[drinks - 1]].first, idx[perm[drinks - 1]].second, bi, bj)
  115.         ;
  116.         if(x == -1) {
  117.             cout << -1 << endl;
  118.             return 0;
  119.         }
  120.         p += x;
  121.         if(result > p) {
  122.             result = p;
  123.         }
  124.        
  125.     } while(next_permutation(perm, perm + drinks));
  126.     cout << result << endl;
  127.     // S 1 2 3 B
  128.     return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement