Naxocist

Maze

May 15th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using pi = pair<int, int>;
  6. using tiii = tuple<int, int, int>;
  7.  
  8. const int mod = 1e9 + 7;
  9. const int N = 155;
  10. int tr[] = {1, -1, 0, 0}, tc[] = {0, 0, 1, -1}, A[N][N], B[N][N], mp[N][N];
  11.  
  12.  
  13. int main() {
  14.     // freopen("input.txt", "r", stdin);
  15.  
  16.     int n, m, x, y, s, e; scanf("%d%d%d%d%d%d", &n, &m, &x, &y, &s, &e);
  17.  
  18.     for(int i=1; i<=n; ++i){
  19.         for(int j=1; j<=m; ++j){
  20.             scanf("%d", &mp[i][j]);
  21.         }
  22.     }
  23.  
  24.     int cnt = 0, mn = 2e9;
  25.     queue<pi> q;
  26.     q.emplace(x, y);
  27.     A[x][y] = 1;
  28.     while(q.size()){
  29.         int u, v;
  30.         tie(u, v) = q.front(); q.pop();
  31.  
  32.         for(int i=0; i<n; ++i){
  33.             int h = tr[i] + u, k = tc[i] + v;
  34.             if(h < 1 || k < 1 || h > n || k > m || A[h][k]) continue ;
  35.  
  36.             if(mp[h][k]) q.emplace(h, k);
  37.  
  38.             A[h][k] = A[u][v] + 1;
  39.         }
  40.  
  41.     }
  42.  
  43.  
  44.     q.emplace(s, e);
  45.     B[s][e] = 1;
  46.     while(q.size()){
  47.         int u, v;
  48.         tie(u, v) = q.front(); q.pop();
  49.  
  50.         for(int i=0; i<n; ++i){
  51.             int h = tr[i] + u, k = tc[i] + v;
  52.             if(h < 1 || k < 1 || h > n || k > m || B[h][k]) continue ;
  53.            
  54.             if(mp[h][k]) q.emplace(h, k);
  55.  
  56.             B[h][k] = B[u][v] + 1;
  57.  
  58.             if(A[h][k] && B[h][k]){
  59.                 cnt++;
  60.                 mn = min(mn, A[h][k] + B[h][k] - 1);
  61.             }
  62.         }
  63.  
  64.     }
  65.  
  66.     printf("%d\n%d", cnt, mn);
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment