Submission #1352227

#TimeUsernameProblemLanguageResultExecution timeMemory
1352227gayMaze (JOI23_ho_t3)C++20
27 / 100
2095 ms37944 KiB
#include <bits/stdc++.h>
#include <experimental/random>
#include <random>

//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;

using namespace std;

using ld = long double;
using ll = long long;

const ll INF = 1e18, MOD = 1e9 + 7;

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q = 1;
    //cin >> q;
    while (q--) {
        solve();
    }
}

vector<pair<ll, ll>> cord = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void solve() {
    ll r, c, n;
    cin >> r >> c >> n;
    ll sr, sc, gr, gc;
    cin >> sr >> sc >> gr >> gc;
    sr--, sc--, gr--, gc--;
    vector<vector<char>> a(r, vector<char>(c));
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> a[i][j];
        }
    }
    if (r > c) {
        swap(r, c);
        swap(sr, sc); swap(gr, gc);
        vector<vector<char>> nw(r, vector<char>(c));
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                nw[i][j] = a[j][i];
            }
        }
        a = nw;
    }

    vector<vector<ll>> dp(r, vector<ll>(c, INF));
    dp[sr][sc] = 0;

    deque<pair<ll, ll>> dj;
    dj.emplace_back(sr, sc);

    vector<vector<ll>> used(r, vector<ll>(c));

    while (!empty(dj)) {
        auto [x, y] = dj.front();
        dj.pop_front();
        for (auto [dx, dy] : cord) {
            ll i = x + dx, j = y + dy;
            if (i < 0 || j < 0 || i == r || j == c || a[i][j] == '#') {
                continue;
            }
            if (dp[i][j] > dp[x][y]) {
                dj.emplace_front(i, j);
                dp[i][j] = dp[x][y];
            }
        }
        for (int i = max(0ll, x - n); i < min(r, x + n + 1); i++) {
            for (int j = max(0ll, y - n); j < min(c, y + n + 1); j++) {
                if (abs(i - x) == n && abs(j - y) == n) {
                    continue;
                }
                if (dp[i][j] > dp[x][y] + 1) {
                    used[i][j]++;
                    dj.emplace_back(i, j);
                    dp[i][j] = dp[x][y] + 1;
                }
            }
        }
    }

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (used[i][j] > 1) {
                exit(-1);
            }
        }
    }

    cout << dp[gr][gc];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...