Submission #1297289

#TimeUsernameProblemLanguageResultExecution timeMemory
1297289the_commando_xMecho (IOI09_mecho)C++20
100 / 100
218 ms6664 KiB
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

using pii = pair<int, int>;
using vii = vector<pii>;

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

using pll = pair<ll, ll>;
using vllll = vector<pll>;

using d = double;
using vd = vector<d>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;

using ld = long double;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;

using vb = vector<bool>;
using vvb = vector<vb>;
using pbb = pair<bool, bool>;
using vbb = vector<pbb>;

#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

vi D = {-1, 0, 1, 0, -1};
void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (!name.empty())
    {
        (void)freopen((name + ".in").c_str(), "r", stdin);
        (void)freopen((name + ".out").c_str(), "w", stdout);
    }
}

const ld pi = 3.14159265358979323846;

const ll LINF = 1e18;
const ll INF = 1e9;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;

const ll MAXN = 800;
vector<string> field(MAXN);

int n, s;

int mX, mY, hX, hY;
vii hives;

vvb bVis(MAXN, vb(MAXN));
vvi bTime(MAXN, vi(MAXN));

bool valid_cell(int x, int y)
{
    return x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M');
}

bool mReached(int distM, int beeDis)
{
    return distM / s < beeDis;
}

void bfsBees()
{
    queue<pii> q;
    for (auto [x, y] : hives)
        q.push({x, y}), bVis[x][y] = true;

    while (q.size())
    {
        auto [x, y] = q.front();
        q.pop();

        for (int d = 0; d < 4; ++d)
        {
            int nx = x + D[d], ny = y + D[d + 1];
            if (valid_cell(nx, ny) && !bVis[nx][ny])
                bVis[nx][ny] = true, bTime[nx][ny] = bTime[x][y] + 1, q.push({nx, ny});
        }
    }
}

bool binarySearchCondition(int t)
{
    vvb mVis(n, vb(n));
    vvi mTime(n, vi(n));

    queue<pii> q;
    q.push({mX, mY}), mVis[mX][mY] = true;

    if (bTime[mX][mY] <= t)
    q.pop();


    while (q.size())
    {
        auto [x, y] = q.front();
        q.pop();

        for (int d = 0; d < 4; ++d)
        {
            int nx = x + D[d], ny = y + D[d + 1];
            if (valid_cell(nx, ny) && !mVis[nx][ny] && mReached(mTime[x][y] + 1, bTime[nx][ny] - t))
                mVis[nx][ny] = true, mTime[nx][ny] = mTime[x][y] + 1, q.push({nx, ny});
        }
    }

    for (int d = 0; d < 4; ++d)
    {
        int nx = hX + D[d], ny = hY + D[d + 1];
        if (valid_cell(nx, ny) && mReached(mTime[nx][ny], bTime[nx][ny] - t) && mVis[nx][ny])
            return true;
    }
    return false;
}

void solve()
{
    cin >> n >> s;

    for (int i = 0; i < n; ++i)
        cin >> field[i];

    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (field[i][j] == 'M')
                mX = i, mY = j;
            else if (field[i][j] == 'D')
                hX = i, hY = j;
            else if (field[i][j] == 'H')
                hives.pb({i, j});

    bfsBees();

    int l = 0, r = n * n;
    while (l <= r)
    {

        int mid = l + (r - l) / 2;
        if (binarySearchCondition(mid))
            l = mid + 1;
        else
            r = mid - 1;
    }

    cout << l - 1;
}

int main()
{
    setIO("");

#ifndef ONLINE_JUDGE
    // setIO("filename");
#endif

    int t = 1;
    // cin >> t;
    while (t--)
        solve();

    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:48:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         (void)freopen((name + ".in").c_str(), "r", stdin);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:49:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         (void)freopen((name + ".out").c_str(), "w", stdout);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...