Advertisement
Josif_tepe

Untitled

Mar 4th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int n, m;
  8.     cin >> n >> m;
  9.     char mat[n][m];
  10.     int si, sj; // za S
  11.     int ei, ej; // za E
  12.     for(int i = 0; i < n; i++) {
  13.         for(int j = 0; j < m; j++) {
  14.             cin >> mat[i][j];
  15.             if(mat[i][j] == 'S') {
  16.                 si = i;
  17.                 sj = j;
  18.             }
  19.             if(mat[i][j] == 'E') {
  20.                 ei = i;
  21.                 ej = j;
  22.             }
  23.         }
  24.     }
  25.     bool visited[n][m];
  26.     for(int i = 0; i < n; i++) {
  27.         for(int j = 0; j < m; j++) {
  28.             visited[i][j] = false;
  29.         }
  30.     }
  31.     // bfs da vidime dali ima direkten pat od S do E
  32.     int di[] = {-1, 1, 0, 0};
  33.     int dj[] = {0, 0, -1, 1};
  34.     queue<int> q;
  35.     q.push(si);
  36.     q.push(sj);
  37.     q.push(0);
  38.     visited[si][sj] = true;
  39.     while(!q.empty()) {
  40.         int ci = q.front();
  41.         q.pop();
  42.         int cj = q.front();
  43.         q.pop();
  44.         int dist = q.front();
  45.         q.pop();
  46.         if(mat[ci][cj] == 'E') {
  47.             cout << dist << endl;
  48.             return 0; // ja zavrsuvame programata bidejki mozeme od S do E da stigneme bez problemi
  49.         }
  50.         for(int k = 0; k < 4; k++) {
  51.             int ti = ci + di[k];
  52.             int tj = cj + dj[k];
  53.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited[ti][tj]) {
  54.                 visited[ti][tj] = true;
  55.                 q.push(ti);
  56.                 q.push(tj);
  57.                 q.push(dist + 1);
  58.             }
  59.         }
  60.     }
  61.     int dist_from_S_to_each_wall[n][m];
  62.     for(int i = 0; i < n; i++) {
  63.         for(int j = 0; j < m; j++) {
  64.             dist_from_S_to_each_wall[i][j] = -1;
  65.             visited[i][j] = false;
  66.         }
  67.     }
  68.     q.push(si);
  69.     q.push(sj);
  70.     q.push(0);
  71.     visited[si][sj] = true;
  72.     while(!q.empty()) {
  73.         int ci = q.front();
  74.         q.pop();
  75.         int cj = q.front();
  76.         q.pop();
  77.         int dist = q.front();
  78.         q.pop();
  79.         if(mat[ci][cj] == '#') {
  80.             dist_from_S_to_each_wall[ci][cj] = dist;
  81.             continue;
  82.         }
  83.         for(int k = 0; k < 4; k++) {
  84.             int ti = ci + di[k];
  85.             int tj = cj + dj[k];
  86.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  87.                 visited[ti][tj] = true;
  88.                 q.push(ti);
  89.                 q.push(tj);
  90.                 q.push(dist + 1);
  91.             }
  92.         }
  93.     }
  94.     int dist_from_E_to_each_wall[n][m];
  95.     for(int i = 0; i < n; i++) {
  96.         for(int j = 0; j < m; j++) {
  97.             dist_from_E_to_each_wall[i][j] = -1;
  98.             visited[i][j] = false;
  99.         }
  100.     }
  101.     q.push(ei);
  102.     q.push(ej);
  103.     q.push(0);
  104.     visited[ei][ej] = true;
  105.     while(!q.empty()) {
  106.         int ci = q.front();
  107.         q.pop();
  108.         int cj = q.front();
  109.         q.pop();
  110.         int dist = q.front();
  111.         q.pop();
  112.         if(mat[ci][cj] == '#') {
  113.             dist_from_E_to_each_wall[ci][cj] = dist;
  114.             continue;
  115.         }
  116.         for(int k = 0; k < 4; k++) {
  117.             int ti = ci + di[k];
  118.             int tj = cj + dj[k];
  119.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  120.                 visited[ti][tj] = true;
  121.                 q.push(ti);
  122.                 q.push(tj);
  123.                 q.push(dist + 1);
  124.             }
  125.         }
  126.     }
  127.     int rezultat = -1;
  128.     for(int i = 0; i < n; i++) {
  129.         for(int j = 0; j < m; j++) {
  130.             if(mat[i][j] == '#') {
  131.                 if(dist_from_S_to_each_wall[i][j] != -1 and dist_from_E_to_each_wall[i][j] != -1) {
  132.                     rezultat = max(rezultat, dist_from_S_to_each_wall[i][j] + dist_from_E_to_each_wall[i][j]);
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     cout << rezultat << endl;
  138.     return 0;
  139. }
  140. /*
  141.  4 5
  142.  S..#E
  143.  ###..
  144.  #....
  145.  .....
  146.  
  147.  */
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement