Advertisement
Josif_tepe

Untitled

Mar 30th, 2024
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. int main() {
  10.     int n, m;
  11.     cin >> n >> m;
  12.    
  13.     vector<vector<char>> mat(n, vector<char>(m));
  14.     bool visited[n][m];
  15.     int si, sj;
  16.     int ei, ej;
  17.     for(int i = 0; i < n; i++) {
  18.         for(int j = 0; j < m; j++) {
  19.             cin >> mat[i][j];
  20.             visited[i][j] = false;
  21.             if(mat[i][j] == 'S') {
  22.                 si = i;
  23.                 sj = j;
  24.             }
  25.             if(mat[i][j] == 'E') {
  26.                 ei = i;
  27.                 ej = j;
  28.             }
  29.         }
  30.     }
  31.     queue<int> q;
  32.     q.push(si);
  33.     q.push(sj);
  34.     q.push(0);
  35.    
  36.     int di[] = {-1, 1, 0, 0};
  37.     int dj[] = {0, 0, -1, 1};
  38.    
  39.     while(!q.empty()) {
  40.         int ci = q.front();
  41.         q.pop();
  42.         int cj = q.front();
  43.         q.pop();
  44.         int cekor = q.front();
  45.         q.pop();
  46.        
  47.         if(ci == ei and cj == ej) {
  48.             cout << cekor << endl;
  49.             return 0;
  50.         }
  51.         for(int i = 0; i < 4; i++) {
  52.             int ti = ci + di[i];
  53.             int tj = cj + dj[i];
  54.            
  55.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited[ti][tj]) {
  56.                 q.push(ti);
  57.                 q.push(tj);
  58.                 q.push(cekor + 1);
  59.                 visited[ti][tj] = true;
  60.             }
  61.         }
  62.     }
  63.     return 0;
  64. }
  65. /**
  66.  
  67.  ....S##...
  68.  E....##..
  69.  ##..##
  70.  ...###...
  71.  
  72.  
  73.  **/
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement