# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659073 | ansgar | Mecho (IOI09_mecho) | C++17 | 216 ms | 16616 KiB |
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;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vb vector<bool>
#define vc vector<char>
#define vvc vector<vc>
#define vvb vector<vb>
#define si set<int>
#define mii map<int,int>
const int mod=1e9+7;
const int N=2e5+1;
const int LN=LLONG_MAX/10;
vvc L;
int n,k;
vvi Bees;
vvi Bear;
vpii BeesLoc;
bool inside(int y,int x){
return (y>=0 and y<n and x>=0 and x<n and L[y][x]!='T');
}
void bfsBees(){
queue<pii> Q;
for(auto B : BeesLoc){
Q.push(B);
Bees[B.first][B.second]=-1;
}
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and Bees[y-1][x]>Bees[y][x]+1){
Bees[y-1][x]=Bees[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and Bees[y+1][x]>Bees[y][x]+1){
Bees[y+1][x]=Bees[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and Bees[y][x-1]>Bees[y][x]+1){
Bees[y][x-1]=Bees[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and Bees[y][x+1]>Bees[y][x]+1){
Bees[y][x+1]=Bees[y][x]+1;
Q.push({y,x+1});
}
}
}
void bfsBear(int sy,int sx,int w){
queue<pii> Q;
Q.push({sy,sx});
Bear[sy][sx]=0;
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and Bear[y-1][x]>Bear[y][x]+1 and Bees[y-1][x]-((Bear[y][x]+1)/k)>=w){
Bear[y-1][x]=Bear[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and Bear[y+1][x]>Bear[y][x]+1 and Bees[y+1][x]-((Bear[y][x]+1)/k)>=w){
Bear[y+1][x]=Bear[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and Bear[y][x-1]>Bear[y][x]+1 and Bees[y][x-1]-((Bear[y][x]+1)/k)>=w){
Bear[y][x-1]=Bear[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and Bear[y][x+1]>Bear[y][x]+1 and Bees[y][x+1]-((Bear[y][x]+1)/k)>=w){
Bear[y][x+1]=Bear[y][x]+1;
Q.push({y,x+1});
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
L=vvc(n,vc(n));
Bear=vvi(n,vi(n,LN));
Bees=vvi(n,vi(n,LN));
int sx,sy,ex,ey;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>L[i][j];
if(L[i][j]=='M')sy=i,sx=j;
if(L[i][j]=='D')ey=i,ex=j;
if(L[i][j]=='H')BeesLoc.emplace_back(i,j);
}
}
bfsBees();
int high=LN;
int low=0;
int res=-1;
while(high>=low){
int mid=(high+low)/2;
Bear=vvi(n,vi(n,LN));
bfsBear(sy,sx,mid);
if(Bear[ey][ex]==LN)high=mid-1;
else{
res=mid;
low=mid+1;
}
}
cout<<res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |