Submission #401055

#TimeUsernameProblemLanguageResultExecution timeMemory
401055ruadhanMecho (IOI09_mecho)C++17
100 / 100
299 ms6436 KiB
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define x first
#define y second
#define X 0
#define Y 1
#define TIME 2
#define STEPS 3
using namespace std;
typedef pair<int, int> pi;

const int MX = 805;
string grid[MX];
int bees_time[MX][MX];
int N, S;

vector<pi> hives;
vector<pi> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void calc_bee_times()
{
    queue<pi> q;
    for (auto i : hives)
    {
        q.push(i);
        bees_time[i.x][i.y] = 0;
    }
    while (!q.empty())
    {
        auto curr = q.front();
        q.pop();
        for (auto d : dirs)
        {
            int i = curr.x + d.x, j = curr.y + d.y;
            if (i < 0 || i >= N || j < 0 || j >= N || grid[i][j] == 'T' || grid[i][j] == 'D' || bees_time[i][j] != -1)
                continue;
            bees_time[i][j] = bees_time[curr.x][curr.y] + 1;
            q.push({i, j});
        }
    }
}

pi mecho, home;
int processed[MX][MX];
bool ok(int t)
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            processed[i][j] = -1;
    }

    if (bees_time[mecho.x][mecho.y] <= t)
        return false;

    queue<pi> q;
    processed[mecho.x][mecho.y] = 0;

    q.push(mecho);
    while (!q.empty())
    {
        auto curr = q.front();

        // cout << "new queue top = " << curr.x << " " << curr.y << ", d=" << processed[curr.x][curr.y] << '\n';

        q.pop();
        for (auto d : dirs)
        {
            int i = curr.x + d.x, j = curr.y + d.y;

            if (i < 0 || i >= N || j < 0 || j >= N || grid[i][j] == 'T' || (bees_time[i][j] <= t + (processed[curr.x][curr.y] + 1) / S && make_pair(i, j) != home) || processed[i][j] != -1)
                continue;

            // cout << "we can go to " << i << ", " << j << " from here with steps = " << processed[curr.x][curr.y] + 1 << ", moves = " << (processed[curr.x][curr.y] + 1) / S << '\n';

            processed[i][j] = processed[curr.x][curr.y] + 1;
            q.push({i, j});
        }
    }
    return processed[home.x][home.y] != -1;
}

int main()
{
    cin >> N >> S;
    for (int i = 0; i < MX; i++)
        for (int j = 0; j < MX; j++)
            bees_time[i][j] = -1;

    for (int i = 0; i < N; i++)
    {
        cin >> grid[i];
        for (int j = 0; j < N; j++)
        {
            if (grid[i][j] == 'H')
                hives.push_back({i, j});
            else if (grid[i][j] == 'M')
                mecho.x = i, mecho.y = j;
            else if (grid[i][j] == 'D')
                home.x = i, home.y = j;
        }
    }

    calc_bee_times();

    // cout << ok(2) << '\n';

    // for (int i = 0; i < N; i++)
    // {
    //     for (int j = 0; j < N; j++)
    //         cout << bees_time[i][j] << " ";
    //     cout << '\n';
    // }

    int lo = 0, hi = 800 * 800;
    for (int b = hi / 2; b >= 1; b /= 2)
    {
        while (lo + b < hi && ok(lo + b))
            lo += b;
    }

    if (lo == 0 && !ok(0))
        cout << -1 << '\n';
    else
        cout << lo << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...