# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
709324 | hafo | Mecho (IOI09_mecho) | C++14 | 865 ms | 9628 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) {
if(t != 2) 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[t][i][j] == -1) {
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) continue;
f[t][x][y] = f[t][u][v] + 1;
q.push({x, y});
}
}
}
bool check(int time) {
bfs(0);
bfs(1);
memset(f[2], -1, sizeof f[2]);
int mn = oo;
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 || a[x][y] == 'T') continue;
if(f[1][x][y] == -1) continue;
mini(mn, (f[1][x][y] + s - 1) / s);
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
if(a[i][j] == 'T' || f[0][i][j] == -1 || f[1][i][j] == -1) continue;
if(f[0][i][j] - (f[1][i][j] + s - 1) / s < time) f[2][i][j] = oo;
else if(f[0][i][j] - (f[1][i][j] + s - 1) / s == time && (f[1][i][j] == 0 || (f[1][i][j] + s - 1) / s != mn)) f[2][i][j] = oo;
}
bfs(2);
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 || a[x][y] == 'T') continue;
if(f[2][x][y] == -1) continue;
if(f[0][x][y] == -1) return 1;
if(f[0][x][y] - (f[2][x][y] + s - 1) / 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... |