# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
492832 | niloyroot | Mecho (IOI09_mecho) | C++14 | 1098 ms | 6628 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;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;
void solve(){
ll n,s; cin>>n>>s;
char c[n+1][n+1];
vector<pl> hives;
ll dis[n+1][n+1]; forp(i,1,n){forp(j,1,n){dis[i][j]=INT_MAX;}}
pl start,end;
forp(i,1,n){
forp(j,1,n){
cin>>c[i][j];
if(c[i][j]=='H'){
hives.pb({i,j});
dis[i][j]=0;
}
if(c[i][j]=='T'){
dis[i][j]=0;
}
if(c[i][j]=='M'){
start.ff=i; start.ss=j;
}
if(c[i][j]=='D'){
end.ff=i; end.ss=j;
}
}
}
bool vis[n+2][n+2];
queue<pl> q;
ll i,j;
for(auto p:hives){
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
q.push(p);
vis[p.ff][p.ss]=1;
while(!q.empty()){
i=q.front().ff; j=q.front().ss;
q.pop();
if(!vis[i][j+1] && (c[i][j+1]!='T' && c[i][j+1]!='H' && c[i][j+1]!='D')){
vis[i][j+1]=1;
dis[i][j+1]=min(dis[i][j+1],dis[i][j]+1);
q.push({i,j+1});
}
if(!vis[i+1][j] && (c[i+1][j]!='T' && c[i+1][j]!='H' && c[i+1][j]!='D')){
vis[i+1][j]=1;
dis[i+1][j]=min(dis[i+1][j],dis[i][j]+1);
q.push({i+1,j});
}
if(!vis[i][j-1] && (c[i][j-1]!='T' && c[i][j-1]!='H' && c[i][j-1]!='D')){
vis[i][j-1]=1;
dis[i][j-1]=min(dis[i][j-1],dis[i][j]+1);
q.push({i,j-1});
}
if(!vis[i-1][j] && (c[i-1][j]!='T' && c[i-1][j]!='H' && c[i-1][j]!='D')){
vis[i-1][j]=1;
dis[i-1][j]=min(dis[i-1][j],dis[i][j]+1);
q.push({i-1,j});
}
}
}
ll l=0,r=dis[start.ff][start.ss]-1,mid,len;
queue<pair<pl,ll>> qq;
while(l<r){
mid=(l+r+1)/2;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
l=mid;
} else {
r=mid-1;
}
}
mid=0;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
cout<<l<<newl;
} else {
cout<<-1<<newl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1; //cin>>t;
while(t--)solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |