# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1100021 | asli_bg | Mecho (IOI09_mecho) | C++11 | 145 ms | 3932 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
int mark[MAXN][MAXN];
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' and grid[x][y]!='M') return false;
if(mark[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;
if(mark[bas.fi][bas.se]<deg) return false;
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];
queue<pii> q;
int say=0;
FOR(i,n){
FOR(j,n){
if(grid[i][j]=='H'){
//hive.insert({i,j});
q.push({i,j});
mark[i][j]=0;
}
else if(grid[i][j]=='M') {
bas={i,j};
say++;
}
else if(grid[i][j]=='D'){
son={i,j};
mark[i][j]=INF;
}
else say++;
}
}
int mx=0;
while(!q.empty()){
auto el=q.front();
q.pop();
FOR(i,4){
int yenix=el.fi+yon1[i];
int yeniy=el.se+yon2[i];
if(isvalid(yenix,yeniy,true)){
mark[yenix][yeniy]=mark[el.fi][el.se]+1;
mx=max(mx,mark[yenix][yeniy]);
q.push({yenix,yeniy});
}
}
}
/*FOR(i,n){
FOR(j,n) cout<<mark[i][j]<<' ';
cout<<endl;
}
cout<<endl;*/
int l=0;
int r=mx+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... |