# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
117106 | eriksuenderhauf | Mecho (IOI09_mecho) | C++11 | 333 ms | 7160 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")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 810;
const double eps = 1e-9;
int a[MAXN][MAXN], vis[MAXN][MAXN];
int n, s, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char str[MAXN][MAXN];
pii st = mp(0, 0), en = mp(0, 0);
bool in(int x, int y) {
if (x < 1 || x > n || y < 1 || y > n)
return 0;
return 1;
}
bool ok(int t) {
mem(vis, 0x3f);
deque<pii> pq;
pq.pb(st);
vis[st.fi][st.se] = 0;
while (!pq.empty()) {
pii cur = pq.front(); pq.pop_front();
if (cur == en)
return 1;
for (int i = 0; i < 4; i++) {
int nx = cur.fi + dx[i];
int ny = cur.se + dy[i];
if (!in(nx, ny) || vis[cur.fi][cur.se] / s + t >= a[cur.fi][cur.se])
continue;
if (vis[nx][ny] > vis[cur.fi][cur.se] + 1) {
vis[nx][ny] = vis[cur.fi][cur.se] + 1;
pq.pb(mp(nx, ny));
}
}
}
return 0;
}
int main() {
mem(a, -1);
scanf("%d %d", &n, &s);
deque<pii> pq;
for (int i = 1; i <= n; i++) {
scanf("%s", str[i] + 1);
for (int j = 1; j <= n; j++) {
if (str[i][j] == 'G' || str[i][j] == 'M' || str[i][j] == 'D')
a[i][j] = INF;
if (str[i][j] == 'M')
st = mp(i, j);
if (str[i][j] == 'D')
en = mp(i, j);
if (str[i][j] == 'H') {
pq.pb(mp(i, j));
a[i][j] = 0;
}
}
}
while (!pq.empty()) {
pii cur = pq.front(); pq.pop_front();
for (int i = 0; i < 4; i++) {
int nx = cur.fi + dx[i];
int ny = cur.se + dy[i];
if (!in(nx, ny) || str[nx][ny] == 'D')
continue;
if (a[nx][ny] > a[cur.fi][cur.se] + 1) {
a[nx][ny] = a[cur.fi][cur.se] + 1;
pq.pb(mp(nx, ny));
}
}
}
int lo = 0, hi = n * n;
while (lo <= hi) {
int mi = (lo+hi) / 2;
if (ok(mi))
lo = mi + 1;
else
hi = mi - 1;
}
pri(lo - 1);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |