#include <bits/stdc++.h>
#define REP(v, i, j) for (int v = i; v != j; v++)
#define FORI(v) for (auto i : v)
#define FORJ(v) for (auto j : v)
#define OUT(v, a) \
FORI(v) \
cout << i << a;
#define OUTS(v, a, b) \
cout << v.size() << a; \
OUT(v, b)
#define in(a, n) \
REP(i, 0, n) \
cin >> a[i];
#define SORT(v) sort(begin(v), end(v))
#define REV(v) reverse(begin(v), end(v))
#define MEMSET(m) memset(m, -1, sizeof m)
#define pb push_back
#define fi first
#define se second
#define detachIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
bool operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
if(__x.size() != __y.size()) return false;
return std::equal(__x.begin(), __x.end(), __y.begin());
}
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;
const int MOD = 1e9+7;
struct modint {
long long val;
modint() = default;
modint(int _val): val(_val){}
modint operator+(modint b){ return ((this->val + b.val)%MOD); }
modint operator-(modint b){ return ((MOD + this->val - b.val)%MOD); }
modint operator*(modint b){ return ((this->val * b.val)%MOD); }
modint operator^(int a){
if(a==0)return 1;
if(a==1)return *this;
return (((*this)*(*this))^(a>>1))*((*this)^(a&1));
}
};
modint invert(modint a){
return a^(MOD-2);
}
modint operator/(modint a, modint b){
return a*invert(b);
}
string grid[200100];
int main(){
detachIO;
int r,c;cin>>r>>c;
int n;cin>>n;
int sr,sc,gr,gc;cin>>sr>>sc>>gr>>gc;
REP(i,0,r){
string s;cin>>s;
grid[i+1]="-"+s;
// cerr<<s<<endl;
}
// cerr<<endl;
deque<pair<pii,int>> q;
q.push_back({{sr,sc},0});
while(q.size()){
auto top=q.front();
q.pop_front();
// cerr<<top.fi.fi<<" "<<top.fi.se<<" "<<top.se<<endl;
if(top.fi==make_pair(gr,gc))cout<<top.se,exit(0);
if(top.fi.fi<0){
top.fi.fi*=-1;
top.fi.se*=-1;
if((top.fi.fi-n)<=gr && gr<=(top.fi.fi+n) && (top.fi.se-n)<=gc && gc<=(top.fi.se+n))cout<<top.se,exit(0);
// char prv = grid[top.fi.fi][top.fi.se];
// grid[top.fi.fi][top.fi.se]='X';
// REP(i,1,r+1){REP(j,1,c+1){cerr<<grid[i][j]<<" ";}cerr<<endl;}
// cerr<<"------"<<endl;
// grid[top.fi.fi][top.fi.se]=prv;
REP(x,max(1,top.fi.fi-n+1),min(top.fi.fi+n-1,r)+1){
int y=min(top.fi.se+n,c);
// cerr<<"TRY "<<x<<" "<<y<<endl;
if(grid[x][y]=='+')continue;
q.push_front({{x,y},top.se});
// cerr<<"ADD "<<x<<" "<<y<<endl;
}
REP(x,max(1,top.fi.fi-n+1),min(top.fi.fi+n-1,r)+1){
int y=max(top.fi.se-n,1);
// cerr<<"TRY "<<x<<" "<<y<<endl;
if(grid[x][y]=='+')continue;
q.push_front({{x,y},top.se});
// cerr<<"ADD "<<x<<" "<<y<<endl;
}
REP(y,max(1,top.fi.se-n+1),min(top.fi.se+n-1,c)+1){
int x=min(top.fi.fi+n,r);
// cerr<<"TRY "<<x<<" "<<y<<endl;
if(grid[x][y]=='+')continue;
q.push_front({{x,y},top.se});
// cerr<<"ADD "<<x<<" "<<y<<endl;
}
REP(y,max(1,top.fi.se-n+1),min(top.fi.se+n-1,c)+1){
int x=max(top.fi.se-n,1);
// cerr<<"TRY "<<x<<" "<<y<<endl;
if(grid[x][y]=='+')continue;
q.push_front({{x,y},top.se});
// cerr<<"ADD "<<x<<" "<<y<<endl;
}
// prv = grid[top.fi.fi][top.fi.se];
// grid[top.fi.fi][top.fi.se]='X';
// REP(i,1,r+1){REP(j,1,c+1){cerr<<grid[i][j]<<" ";}cerr<<endl;}
// cerr<<" "<<endl;
// grid[top.fi.fi][top.fi.se]=prv;
} else {
if(grid[top.fi.fi][top.fi.se]=='+')continue;
grid[top.fi.fi][top.fi.se]='+';
if(top.fi.fi!=1 && grid[top.fi.fi-1][top.fi.se]=='.')q.push_front({{top.fi.fi-1,top.fi.se},top.se});
if(top.fi.fi!=r && grid[top.fi.fi+1][top.fi.se]=='.')q.push_front({{top.fi.fi+1,top.fi.se},top.se});
if(top.fi.se!=1 && grid[top.fi.fi][top.fi.se-1]=='.')q.push_front({{top.fi.fi,top.fi.se-1},top.se});
if(top.fi.se!=c && grid[top.fi.fi][top.fi.se+1]=='.')q.push_front({{top.fi.fi,top.fi.se+1},top.se});
q.push_back({{-top.fi.fi,-top.fi.se},top.se+1});
}
}
// REP(i,1,r+1){REP(j,1,c+1){cerr<<grid[i][j]<<" ";}cerr<<endl;}
assert(0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |