# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
198705 | SamAnd | Mecho (IOI09_mecho) | C++17 | 388 ms | 8420 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>
using namespace std;
const int N = 802;
const int xx[4] = {0, 1, 0, -1};
const int yy[4] = {1, 0, -1, 0};
struct ban
{
int x, y;
ban(){}
ban(int x, int y)
{
this->x = x;
this->y = y;
}
};
int n, s;
int mx, my;
vector<ban> hh;
char a[N][N];
bool ch[N][N];
int dh[N][N];
bool cm[N][N];
int dm[N][N];
bool stg(int x)
{
memset(ch, false, sizeof ch);
memset(cm, false, sizeof cm);
queue<ban> qh;
queue<ban> qm;
for (int i = 0; i < hh.size(); ++i)
{
ch[hh[i].x][hh[i].y] = true;
qh.push(hh[i]);
}
while (!qh.empty())
{
ban t = qh.front();
if (dh[t.x][t.y] == x)
break;
qh.pop();
for (int i = 0; i < 4; ++i)
{
ban h = t;
h.x += xx[i];
h.y += yy[i];
if (h.x >= 0 && h.x < n && h.y >= 0 && h.y < n)
{
if (a[h.x][h.y] == 'G' && !ch[h.x][h.y])
{
ch[h.x][h.y] = true;
dh[h.x][h.y] = dh[t.x][t.y] + 1;
qh.push(h);
}
}
}
}
qm.push(ban(mx, my));
cm[mx][my] = true;
for (int ii = 0; ; ++ii)
{
if (qm.empty())
return false;
while (!qm.empty())
{
ban t = qm.front();
if (dm[t.x][t.y] == (ii + 1) * s)
break;
qm.pop();
if (ch[t.x][t.y])
continue;
if (a[t.x][t.y] == 'D')
return true;
for (int i = 0; i < 4; ++i)
{
ban h = t;
h.x += xx[i];
h.y += yy[i];
if (h.x >= 0 && h.x < n && h.y >= 0 && h.y < n)
{
if ((a[h.x][h.y] == 'G' || a[h.x][h.y] == 'D') && !cm[h.x][h.y] && !ch[h.x][h.y])
{
cm[h.x][h.y] = true;
dm[h.x][h.y] = dm[t.x][t.y] + 1;
qm.push(h);
}
}
}
}
while (!qh.empty())
{
ban t = qh.front();
if (dh[t.x][t.y] == x + ii + 1)
break;
qh.pop();
for (int i = 0; i < 4; ++i)
{
ban h = t;
h.x += xx[i];
h.y += yy[i];
if (h.x >= 0 && h.x < n && h.y >= 0 && h.y < n)
{
if (a[h.x][h.y] == 'G' && !ch[h.x][h.y])
{
ch[h.x][h.y] = true;
dh[h.x][h.y] = dh[t.x][t.y] + 1;
qh.push(h);
}
}
}
}
}
}
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &s);
for (int i = 0; i < n; ++i)
{
scanf(" %s", a[i]);
for (int j = 0; j < n; ++j)
{
if (a[i][j] == 'M')
{
mx = i;
my = j;
a[i][j] = 'G';
}
if (a[i][j] == 'H')
{
hh.push_back(ban(i, j));
a[i][j] = 'G';
}
}
}
int l = 0, r = n * n;
int ans = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (stg(m))
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |