Submission #529621

#TimeUsernameProblemLanguageResultExecution timeMemory
529621thezomb1eMecho (IOI09_mecho)C++17
57 / 100
381 ms6308 KiB
//thatsramen

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)

using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;

//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
const ll dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const ld PI = 3.14159265359;

//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set

void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
    unsyncIO(); setPrec();
    if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}

//#define TEST_CASES

void solve() {
    int n, s;
    cin >> n >> s;
    vector<vector<char>> grid(n, vector<char> (n));
    pair<int, int> start, home;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> grid[i][j];
            if (grid[i][j] == 'D') {
                home = {i, j};
            }
            if (grid[i][j] == 'M') {
                start = {i, j};
            }
        }
    }
    queue<pair<int, int>> que;
    vector<vector<int>> d1(n, vector<int> (n)), d2(n, vector<int> (n));
    auto ok = [&](int x, int y) {
        return x >= 0 && y >= 0 && x < n && y < n && grid[x][y] == 'G';
    };
    int lo = 0, hi = 2000, ans = -1;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d1[i][j] = IINF;
                d2[i][j] = IINF;
                if (grid[i][j] == 'H') {
                    que.push({i, j});
                    d2[i][j] = 0;
                }
            }
        }
        while (!que.empty()) {
            auto cur = que.front();
            que.pop();
            int x = cur.ft, y = cur.sd;
            for (int i = 0; i < 4; i++) {
                int tox = x + dx[i], toy = y + dy[i];
                if (ok(tox, toy) && d2[tox][toy] > d2[x][y] + 1) {
                    d2[tox][toy] = d2[x][y] + 1;
                    que.push({tox, toy});
                }
            }
        }
        if (d2[start.ft][start.sd] > mid) {
            d1[start.ft][start.sd] = 0;
            que.push(start);
        }
        while (!que.empty()) {
            auto cur = que.front();
            que.pop();
            int x = cur.ft, y = cur.sd;
            for (int i = 0; i < 4; i++) {
                int tox = x + dx[i], toy = y + dy[i];
                if (ok(tox, toy) && d1[tox][toy] > d1[x][y] + 1) {
                    if ((mid + (d1[x][y] + 1) / s) < d2[tox][toy]) {
                        d1[tox][toy] = d1[x][y] + 1;
                        que.push({tox, toy});
                    }
                }
            }
        }
        bool can = false;
        for (int i = 0; i < 4; i++) {
            int tox = home.ft + dx[i], toy = home.sd + dy[i];
            if (ok(tox, toy) && d1[tox][toy] != IINF) {
                can = true;
            }
        }
        if (can) {
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid - 1;
        }
    }
    cout << ans << '\n';
}

int main() {
    setIO();

    int tt = 1;
    #ifdef TEST_CASES
        cin >> tt;
    #endif

    while (tt--)
        solve();

    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void setIn(std::string)':
mecho.cpp:43:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void setOut(std::string)':
mecho.cpp:44:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...