Josif_tepe

Untitled

Mar 10th, 2026
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = 1005;
  9. int n, m;
  10. char mat[maxn][maxn];
  11. int main()
  12. {
  13.     cin >> n >> m;
  14.    
  15.     int si, sj, ei, ej;
  16.     for(int i = 0; i < n; i++) {
  17.         for(int j = 0; j < m; j++) {
  18.             cin >> mat[i][j];
  19.            
  20.             if(mat[i][j] == 'S') {
  21.                 si = i;
  22.                 sj = j;
  23.             }
  24.            
  25.             if(mat[i][j] == 'E') {
  26.                 ei = i;
  27.                 ej = j;
  28.             }
  29.         }
  30.     }
  31.    
  32.     vector<vector<int>> dist_from_S(n, vector<int>(m, -1));
  33.     vector<vector<bool>> visited(n, vector<bool>(m, false));
  34.     queue<int> q;
  35.     q.push(si);
  36.     q.push(sj);
  37.     q.push(0);
  38.    
  39.     visited[si][sj] = true;
  40.    
  41.     int di[] = {-1, 1, 0, 0};
  42.     int dj[] = {0, 0, -1, 1};
  43.     while(!q.empty()) {
  44.         int ci = q.front(); q.pop();
  45.         int cj = q.front(); q.pop();
  46.         int dist = q.front(); q.pop();
  47.        
  48.         if(mat[ci][cj] == '#') {
  49.             dist_from_S[ci][cj] = dist;
  50.             continue;
  51.         }
  52.        
  53.         if(mat[ci][cj] == 'E') {
  54.             cout << dist << endl;
  55.             return 0;
  56.         }
  57.         for(int k = 0; k < 4; k++) {
  58.             int ti = ci + di[k];
  59.             int tj = cj + dj[k];
  60.            
  61.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  62.                 q.push(ti);
  63.                 q.push(tj);
  64.                 q.push(dist + 1);
  65.                 visited[ti][tj] = true;
  66.             }
  67.         }
  68.        
  69.     }
  70.    
  71.     visited = vector<vector<bool>>(n, vector<bool>(m, false));
  72.     vector<vector<int>> dist_from_E(n, vector<int>(m, -1));
  73.     q.push(ei);
  74.     q.push(ej);
  75.     q.push(0);
  76.    
  77.     while(!q.empty()) {
  78.         int ci = q.front(); q.pop();
  79.         int cj = q.front(); q.pop();
  80.         int dist = q.front(); q.pop();
  81.        
  82.         if(mat[ci][cj] == '#') {
  83.             dist_from_E[ci][cj] = dist;
  84.             continue;
  85.         }
  86.        
  87.         for(int k = 0; k < 4; k++) {
  88.             int ti = ci + di[k];
  89.             int tj = cj + dj[k];
  90.            
  91.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  92.                 q.push(ti);
  93.                 q.push(tj);
  94.                 q.push(dist + 1);
  95.                 visited[ti][tj] = true;
  96.             }
  97.         }
  98.     }
  99.     int res = -1;
  100.     for(int i = 0; i < n; i++) {
  101.         for(int j = 0; j < m; j++) {
  102.             if(dist_from_E[i][j] != -1 and dist_from_S[i][j] != -1) {
  103.                 res = max(res, dist_from_E[i][j] + dist_from_S[i][j]);
  104.             }
  105.         }
  106.     }
  107.     cout << res << endl;
  108.    
  109.    
  110.    
  111.    
  112.     return 0;
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment