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>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=6e6+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
string arr[maxn];
int r,c,n;
int to(int i,int j){
return i*c+j;
}
bool isin(int x,int y){
if(x<0 or x>=r or y<0 or y>=c) return false;
return true;
}
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int mxx[maxn],mnx[maxn],mxy[maxn],mny[maxn];
vector<pii> bord[maxn];
bool vis[maxn];
pii dst[maxn];
int main(){
cin>>r>>c>>n;
int sx,sy,a,b;
cin>>sx>>sy>>a>>b;
--sx,--sy,--a,--b;
--n;
REP(i,r*c) dst[i]={INF,INF};
REP(i,r){
cin>>arr[i];
}
queue<pii> q;
q.push({sx,sy});
int ans=0;
while(!vis[to(a,b)]){
vector<pii> nw;
while(sz(q)){
pii z=q.front();
nw.pb(z);
q.pop();
REP(k,4){
int x=dx[k]+z.f,y=dy[k]+z.s;
if(!isin(x,y) or arr[x][y]=='#' or vis[to(x,y)]) continue;
vis[to(x,y)]=1;
q.push({x,y});
}
}
if(vis[to(a,b)]) break;
queue<pii> q2;
for(auto z:nw){
REP(k,4){
int x=dx[k]+z.f,y=dy[k]+z.s;
if(!isin(x,y) or arr[x][y]=='.' or vis[to(x,y)]) continue;
vis[to(x,y)]=1;
dst[to(x,y)]={0,0};
q2.push({x,y});
}
}
while(sz(q2)){
pii z=q2.front();
//cout<<z.f<<' '<<z.s<<'\n';
q2.pop();
q.push(z);
REP(k,4){
int x=dx[k]+z.f,y=dy[k]+z.s;
int ndx=dst[to(z.f,z.s)].f+abs(dx[k]),ndy=dst[to(z.f,z.s)].s+abs(dy[k]);
//cout<<isin(x,y)<<' '<<(dst[to(x,y)]<tmp)<<' '<<bad(ndx,ndy)<<'\n';
if(!isin(x,y) or vis[to(x,y)] or ndx>n or ndy>n) continue;
vis[to(x,y)]=1;
dst[to(x,y)]={ndx,ndy};
q2.push({x,y});
//cout<<x<<' '<<y<<'\n';
}
}
++ans;
}
cout<<ans<<'\n';
}
# | 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... |