Advertisement
Guest User

2 problem

a guest
Oct 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. const int val = (1e6) + 5;
  5.  
  6. using namespace std;
  7.  
  8. int n, m, k;
  9. int a[1005][1005], dis[val];
  10. vector <int> g[val];
  11. bool used[val];
  12. queue <int> q;
  13.  
  14.  
  15.  
  16. int f(int x, int y){
  17.     return (x - 1) * n + y;
  18. }
  19.  
  20.  
  21. int main(){
  22.     ios_base::sync_with_stdio(0);
  23.     freopen ("in.txt", "r", stdin);
  24.     freopen ("out.txt", "w", stdout);
  25.     cin >> n >> m;
  26.     for(int i=1; i<=n; ++i){
  27.         for(int j=1; j<=m; ++j){
  28.             cin >> a[i][j];
  29.         }
  30.     }
  31.     cin >> k;
  32.     int x1, y1, x2, y2;
  33.     cin >> x1 >> y1 >> x2 >> y2;
  34.     for(int i=1; i<=n; ++i){
  35.         for(int j=1; j<=m; ++j){
  36.             if(i - 1 > 0 && abs(a[i][j] - a[i-1][j]) <= k){
  37.                 g[f(i, j)].push_back(f(i-1, j));
  38.             }
  39.             if(j - 1 > 0 && abs(a[i][j] - a[i][j-1]) <= k){
  40.                 g[f(i, j)].push_back(f(i, j-1));
  41.             }
  42.             if(i + 1 <= n && abs(a[i][j] - a[i+1][j]) <= k){
  43.                 g[f(i, j)].push_back(f(i+1, j));
  44.             }
  45.             if(j + 1 <= m && abs(a[i][j] - a[i][j+1]) <= k){
  46.                 g[f(i, j)].push_back(f(i, j+1));
  47.             }
  48.         }
  49.     }
  50.     q.push(f(x1, y1));
  51.     used[f(x1, y1)] = true;
  52.     while(!q.empty()){
  53.         int v = q.front();
  54.         q.pop();
  55.         for(auto to : g[v]){
  56.             if(!used[to]){
  57.                 used[to] = true;
  58.                 dis[to] = dis[v] + 1;
  59.                 q.push(to);
  60.             }
  61.         }
  62.     }
  63.     if(used[f(x2, y2)]){
  64.         cout << "Yes" << endl;
  65.         cout << dis[f(x2, y2)] << endl;
  66.     }
  67.     else cout << "No" << endl;
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement