Josif_tepe

Untitled

Sep 16th, 2025
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <set>
  7. #include <map>
  8. #include <cstring>
  9. //#include <bits/stdc++.h>
  10. using namespace std;
  11. typedef long long ll;
  12. const int maxn = 55;
  13. int n, m;
  14. char mat[maxn][maxn];
  15.  
  16. int bfs(vector<pair<int, int>> S, vector<pair<int, int>> E, char c) {
  17.     queue<vector<pair<int, int>>> q;
  18.     q.push(S);
  19.    
  20.     queue<int> q_dist;
  21.     q_dist.push(0);
  22.    
  23.     map<vector<pair<int, int>>, bool> visited;
  24.     int di[] = {-1, 1, 0, 0};
  25.     int dj[] = {0, 0, -1, 1};
  26.    
  27.     while(!q.empty()) {
  28.         vector<pair<int, int>> piano = q.front();
  29.         q.pop();
  30.         int dist = q_dist.front();
  31.         q_dist.pop();
  32.        
  33.         if(piano == E) {
  34.             return dist;
  35.         }
  36.        
  37.         for(int i = 0; i < 4; i++) {
  38.             vector<pair<int, int>> t_piano = piano;
  39.             bool ok = true;
  40.             for(int j = 0; j < (int) t_piano.size(); j++) {
  41.                 t_piano[j].first += di[i];
  42.                 t_piano[j].second += dj[i];
  43.                
  44.                 if(t_piano[j].first < 0 or t_piano[j].first >= n or t_piano[j].second < 0 or t_piano[j].second >= m) {
  45.                     ok = false;
  46.                     break;
  47.                 }
  48.                 if(mat[t_piano[j].first][t_piano[j].second] == '#') {
  49.                     ok = false;
  50.                     break;
  51.                 }
  52.                 if(isdigit(mat[t_piano[j].first][t_piano[j].second]) and mat[t_piano[j].first][t_piano[j].second] != c) {
  53.                     ok = false;
  54.                     break;
  55.                 }
  56.             }
  57.             if(ok and !visited[t_piano]) {
  58.                 q.push(t_piano);
  59.                 q_dist.push(dist + 1);
  60.                 visited[t_piano] = true;
  61.             }
  62.         }
  63.     }
  64.     return 2e9;
  65. }
  66.  
  67. int main() {
  68.     ios_base::sync_with_stdio(false);
  69.     vector<pair<int, int>> F, one, two, three;
  70.     cin >> m >> n;
  71.     for(int i = 0; i < n; i++) {
  72.         for(int j = 0; j < m; j++) {
  73.             cin >> mat[i][j];
  74.            
  75.             if(mat[i][j] == 'F') {
  76.                 F.push_back({i, j});
  77.             }
  78.             if(mat[i][j] == '1') {
  79.                 one.push_back({i, j});
  80.             }
  81.             if(mat[i][j] == '2') {
  82.                 two.push_back({i, j});
  83.             }
  84.             if(mat[i][j] == '3') {
  85.                three.push_back({i, j});
  86.             }
  87.            
  88.         }
  89.     }
  90.    
  91.     int eden = bfs(one, F, '1');
  92.     int dva = bfs(two, F, '2');
  93.     int tri = bfs(three, F, '3');
  94.    
  95.     int res = min(eden, min(dva, tri));
  96.     if(res == eden) {
  97.         cout << "1 ";
  98.     }
  99.     else if(res == dva) {
  100.         cout << "2 ";
  101.     }
  102.     else {
  103.         cout << "3 ";
  104.     }
  105.     cout << res << endl;
  106.        
  107.    
  108.    
  109.     return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment