Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- const int val = (1e6) + 5;
- using namespace std;
- int n, m, k;
- int a[1005][1005], dis[val];
- vector <int> g[val];
- bool used[val];
- queue <int> q;
- int f(int x, int y){
- return (x - 1) * n + y;
- }
- int main(){
- ios_base::sync_with_stdio(0);
- freopen ("in.txt", "r", stdin);
- freopen ("out.txt", "w", stdout);
- cin >> n >> m;
- for(int i=1; i<=n; ++i){
- for(int j=1; j<=m; ++j){
- cin >> a[i][j];
- }
- }
- cin >> k;
- int x1, y1, x2, y2;
- cin >> x1 >> y1 >> x2 >> y2;
- for(int i=1; i<=n; ++i){
- for(int j=1; j<=m; ++j){
- if(i - 1 > 0 && abs(a[i][j] - a[i-1][j]) <= k){
- g[f(i, j)].push_back(f(i-1, j));
- }
- if(j - 1 > 0 && abs(a[i][j] - a[i][j-1]) <= k){
- g[f(i, j)].push_back(f(i, j-1));
- }
- if(i + 1 <= n && abs(a[i][j] - a[i+1][j]) <= k){
- g[f(i, j)].push_back(f(i+1, j));
- }
- if(j + 1 <= m && abs(a[i][j] - a[i][j+1]) <= k){
- g[f(i, j)].push_back(f(i, j+1));
- }
- }
- }
- q.push(f(x1, y1));
- used[f(x1, y1)] = true;
- while(!q.empty()){
- int v = q.front();
- q.pop();
- for(auto to : g[v]){
- if(!used[to]){
- used[to] = true;
- dis[to] = dis[v] + 1;
- q.push(to);
- }
- }
- }
- if(used[f(x2, y2)]){
- cout << "Yes" << endl;
- cout << dis[f(x2, y2)] << endl;
- }
- else cout << "No" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement