# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1100015 | asli_bg | Mecho (IOI09_mecho) | C++11 | 1087 ms | 26360 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.
#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define inset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vii;
#define cont(x) for(auto el:x) {cout<<el<<' ';} cout<<endl;
//#define endl '\n'
#define contp(x) for(auto el:x) {cout<<el.fi<<'-'<<el.se<<' ';} cout<<endl;
#define pb push_back
#define sp <<' '<<
#define DEBUG(x) {cout<<(#x) sp x<<endl;}
const int MAXN=805;
const int INF=1e9;
int n,s;
vector<string> grid;
set<pii> ari;
int mark[MAXN][MAXN];
set<pii> hive;
pii bas,son;
int yon1[4]={1,-1,0,0};
int yon2[4]={0,0,-1,1};
//sag, sol, asagi, yukari
bool isvalid(int x,int y,bool f){
if(x>=n or y>=n or x<0 or y<0) return false;
if(f){
if(grid[x][y]!='G') return false;
if(hive.count({x,y})) return false;
}
else{
if(grid[x][y]!='G' and grid[x][y]!='D') return false;
}
return true;
}
bool check(int deg){
//düz shortest path bfs
vector<vb> vis(n+1, vb(n+1,false));
queue<pair<pii,int>> q;
q.push({bas,0});
vis[bas.fi][bas.se]=true;
//cout<<"DEEEEEG" sp deg<<endl;
while(!q.empty()){
auto el=q.front();
q.pop();
int adim=el.se;
int x=el.fi.fi;
int y=el.fi.se;
//cout<<"control" sp adim sp x sp y<<endl;
FOR(i,4){
int yenix=x+yon1[i];
int yeniy=y+yon2[i];
if(!isvalid(yenix,yeniy,false)) continue;
if(vis[yenix][yeniy]) continue;
int sure=adim/s;
//DEBUG(sure);
//DEBUG(yenix);
//DEBUG(yeniy);
if(mark[yenix][yeniy]<deg+sure) continue;
if((adim+1)%s==0){
if(mark[yenix][yeniy]==deg+sure) continue;
}
q.push({{yenix,yeniy},adim+1});
vis[yenix][yeniy]=true;
}
}
return vis[son.fi][son.se];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
grid.resize(n);
FOR(i,n) cin>>grid[i];
FOR(i,n){
FOR(j,n){
if(grid[i][j]=='H'){
hive.insert({i,j});
mark[i][j]=0;
}
if(grid[i][j]=='M') {
bas={i,j};
mark[i][j]=INF;
}
if(grid[i][j]=='D'){
son={i,j};
mark[i][j]=INF;
}
}
}
for(int i=0;true;i++){ //x kez ilerle
set<pii> tut;
for(auto el:hive){
FOR(i,4){
int yenix=el.fi+yon1[i];
int yeniy=el.se+yon2[i];
if(isvalid(yenix,yeniy,true)){
tut.insert({yenix,yeniy});
}
}
}
if(tut.empty()) break;
for(auto el:tut){
mark[el.fi][el.se]=i+1;
hive.insert(el);
}
}
/*FOR(i,n){
FOR(j,n) cout<<mark[i][j]<<' ';
cout<<endl;
}
cout<<endl;*/
int l=0;
int r=n+1;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
cout<<l-1<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |