This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18;
const ll mod=998244353;
const ll N=+7;
const ll M=1e5+7;
const ld eps=1e-9;
ll xd[4]={1,-1,0,0},yd[4]={0,0,1,-1};
vector<vll> d,a;
ll n,m,k;
bool ok(ll x,ll y){
return x>0 && y>0 && x<=n && y<=m;
}
void bfs(ll x,ll y){
queue<pair<ll,ll>> q1;
queue<arr<ll,4>> q2;
q1.push({x,y});
d[x][y]=0;
ll i;
while(q1.sz || q2.sz){
while(q1.sz){
ll x=q1.front().fr,y=q1.front().sc;
q1.pop();
for(i=0;i<4;i++){
ll nx=x+xd[i],ny=y+yd[i];
if(!ok(nx,ny))continue;
if(a[nx][ny]==0){
if(d[nx][ny]>d[x][y]){
d[nx][ny]=d[x][y];
q1.push({nx,ny});
}
}else{
if(d[nx][ny]>d[x][y]+1){
d[nx][ny]=d[x][y]+1;
q2.push({nx,ny,k,k});
}
}
}
}
while(q2.sz){
ll x=q2.front()[0],y=q2.front()[1],sqx=q2.front()[2],sqy=q2.front()[3];
q2.pop();
if(sqx==1 || sqy==1){
q1.push({x,y});
}
for(i=0;i<4;i++){
ll nx=x+xd[i],ny=y+yd[i];
if(!ok(nx,ny))continue;
ll nsqx=sqx;
ll nsqy=sqy;
if(i<2) nsqx--;
else nsqy--;
if(nsqx<=0 || nsqy<=0) continue;
if(d[nx][ny]>d[x][y]){
d[nx][ny]=d[x][y];
q2.push({nx,ny,nsqx,nsqy});
}
}
}
}
}
void solve(){
ll i,j,q;
cin>>n>>m>>k;
ll sx,sy,tx,ty;
cin>>sx>>sy>>tx>>ty;
a.resize(n+5);
d.resize(n+5);
for(i=1;i<=n;i++){
a[i].resize(m+5);
d[i].assign(m+5,inf);
for(j=1;j<=m;j++){
char ch;
cin>>ch;
a[i][j]=(ch=='.' ? 0 : 1);
}
}
bfs(sx,sy);
cout<<d[tx][ty];
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
7 10
1 2
1 3
4 2
100 1
100 2
100 3
300 2
*/
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:101:12: warning: unused variable 'q' [-Wunused-variable]
101 | ll i,j,q;
| ^
# | 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... |