# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
499619 | _AnKahng | Mecho (IOI09_mecho) | C++14 | 147 ms | 4612 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>
#define fi first
#define sc second
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int H[] = {-1,-1,1,1};
const int M = 805;
int n,s,f[M][M];
bool cx[M][M];
pair<int,int> st,fn;
string a[M];
struct dota{
int x,y,d;
};
void BFS_hive(){
queue<dota> q;
memset(cx,false,sizeof(cx));
fo(i,1,n)
fo(j,1,n) {
if(a[i][j] != 'T') cx[i][j] = true;
if(a[i][j] == 'H') q.push({i,j,0}), f[i][j] = 0, cx[i][j] = false;
}
while(!q.empty()){
int x = q.front().x, y = q.front().y, d = q.front().d;
q.pop();
f[x][y] = d;
int xi,yi;
fo(k,0,3){
if(k % 2) xi = x + H[k], yi = y;
else xi = x, yi = y + H[k];
if(cx[xi][yi]) q.push({xi,yi,d+1}), cx[xi][yi] = false;
}
}
}
bool check(int t){
queue<dota> q;
memset(cx,false,sizeof(cx));
fo(i,1,n)
fo(j,1,n) if(a[i][j] != 'T') cx[i][j] = true;
q.push({st.fi,st.sc,0});
cx[st.fi][st.sc] = false;
while(!q.empty()){
int x = q.front().x, y = q.front().y, d = q.front().d;
q.pop();
if(x == fn.fi && y == fn.sc) return true;
int xi,yi;
fo(k,0,3){
if(k % 2) xi = x + H[k], yi = y;
else xi = x, yi = y + H[k];
if(cx[xi][yi] && (d + 1) / s < f[xi][yi] - t) q.push({xi,yi,d+1}), cx[xi][yi] = false;
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> s;
fo(i,1,n) cin >> a[i], a[i] = "*" + a[i];
fo(i,1,n)
fo(j,1,n)
if(a[i][j] == 'M') st = {i,j};
else if(a[i][j] == 'D') fn = {i,j};
BFS_hive();
//fo(i,1,n) fo(j,1,n) cout << f[i][j]<<" \n"[j == n];
int l = 0, r = n, mid,res;
while(l <= r){
mid = (l + r) / 2;
if(check(mid)) l = mid+1, res = mid;
else r = mid-1;
}
cout << res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |