Josif_tepe

Untitled

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