# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
710425 | hafo | Mecho (IOI09_mecho) | C++14 | 474 ms | 6456 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 ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 8e2 + 7;
const ll oo = 1e9 + 69;
int n, s, xd, yd, f[3][maxn][maxn];
char a[maxn][maxn];
pa d[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void bfs(int t, int time) {
memset(f[t], -1, sizeof f[t]);
queue<pa> q;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
if(a[i][j] == 'M' && t != 0 && f[0][i][j] > time) {
f[t][i][j] = 0;
q.push({i, j});
}
if(a[i][j] == 'H' && t == 0) {
f[t][i][j] = 0;
q.push({i, j});
}
}
while(!q.empty()) {
int u = q.front().fi, v = q.front().se;
q.pop();
for(int k = 0; k < 4; k++) {
int x = u + d[k].fi;
int y = v + d[k].se;
if(x < 0 || x == n || y < 0 || y == n || a[x][y] == 'T' || f[t][x][y] != -1 || a[x][y] == 'H') continue;
if(t && f[0][x][y] != -1 && f[0][x][y] - (f[1][u][v] + 1) / s < time) continue;
f[t][x][y] = f[t][u][v] + 1;
q.push({x, y});
}
}
}
bool check(int time) {
bfs(0, time);
bfs(1, time);
for(int k = 0; k < 4; k++) {
int x = xd + d[k].fi;
int y = yd + d[k].se;
if(x < 0 || x == n || y < 0 || y == n || f[0][x][y] == -1 || f[1][x][y] == -1) continue;
if(f[0][x][y] - f[1][x][y] / s > time) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>s;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
cin>>a[i][j];
if(a[i][j] == 'D') {
xd = i;
yd = j;
}
}
int l = 0, r = n * n, mid, res = -1;
while(l <= r) {
mid = l + r >> 1;
if(check(mid)) {
l = mid + 1;
res = mid;
} else r = mid -1 ;
}
cout<<res;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |