Submission #198705

#TimeUsernameProblemLanguageResultExecution timeMemory
198705SamAndMecho (IOI09_mecho)C++17
100 / 100
388 ms8420 KiB
#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)

mecho.cpp: In function 'bool stg(int)':
mecho.cpp:33:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < hh.size(); ++i)
                     ~~^~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &s);
     ~~~~~^~~~~~~~~~~~~~~~
mecho.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %s", a[i]);
         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...