Submission #1352253

#TimeUsernameProblemLanguageResultExecution timeMemory
1352253gayMaze (JOI23_ho_t3)C++20
94 / 100
2105 ms311676 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 int INF = 1e9, 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<int, int>> cord = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void solve() {
    int r, c, n;
    cin >> r >> c >> n;
    int 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;
    }

    if (n >= r) {
        queue<pair<int, int>> bfs;
        bfs.emplace(sr, sc);
        vector<vector<int>> used(r, vector<int>(c));
        used[sr][sc] = 1;
        int mn = sc, mx = sc;
        while (!empty(bfs)) {
            auto [i, j] = bfs.front();
            bfs.pop();
            mn = min(mn, j); mx = max(mx, j);
            for (auto [fx, fy] : cord) {
                int x = i + fx, y = j + fy;
                if (x < 0 || y < 0 || x == r || y == c || a[x][y] == '#') {
                    continue;
                }
                if (used[x][y]) {
                    continue;
                }
                used[x][y] = 1;
                bfs.emplace(x, y);
            }
        }
        int col = 0;
        int lf = sc + 1, rf = sc - 1;
        while (!used[gr][gc]) {
            int nwl = max(0, mn - n), nwr = min(c - 1, mx + n);
            for (int i = 0; i < r; i++) {
                for (int j = nwl; j < lf; j++) {
                    used[i][j] = 1;
                }
                for (int j = rf + 1; j <= nwr; j++) {
                    used[i][j] = 1;
                }
            }
            col++;
            lf = nwl, rf = nwr;
            for (int i = 0; i < r; i++) {
                bfs.emplace(i, lf);
                bfs.emplace(i, rf);
            }
            while (!empty(bfs)) {
                auto [i, j] = bfs.front();
                bfs.pop();
                mn = min(mn, j); mx = max(mx, j);
                for (auto [fx, fy] : cord) {
                    int x = i + fx, y = j + fy;
                    if (x < 0 || y < 0 || x == r || y == c || a[x][y] == '#') {
                        continue;
                    }
                    if (used[x][y]) {
                        continue;
                    }
                    used[x][y] = 1;
                    bfs.emplace(x, y);
                }
            }
        }
        cout << col;
        return;
    }

    vector<vector<int>> used(r, vector<int>(c));
    vector<set<int>> hv(r);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            hv[i].insert(j);
        }
    }
    int col = 0;
    queue<pair<int, int>> bfs;
    bfs.emplace(sr, sc);
    used[sr][sc] = 1;
    hv[sr].erase(sc);
    vector<pair<int, int>> elems;

    while (!bfs.empty()) {
        auto [i, j] = bfs.front();
        bfs.pop();
        elems.emplace_back(i, j);
        for (auto [fx, fy] : cord) {
            int x = i + fx, y = j + fy;
            if (x < 0 || y < 0 || x == r || y == c || a[x][y] == '#') {
                continue;
            }
            if (used[x][y]) {
                continue;
            }
            used[x][y] = 1;
            hv[x].erase(y);
            bfs.emplace(x, y);
        }
    }

    while (!used[gr][gc]) {
        for (auto [x, y] : elems) {
            for (int i = max(0, x - n); i <= min(r - 1, x + n); i++) {
                int lf = max(0, y - n), rf = min(c - 1, y + n);
                if (abs(x - i) == n) {
                    lf = max(0, y - n + 1), rf = min(c - 1, y + n - 1);
                }
                auto it = hv[i].lower_bound(lf);
                vector<int> del;
                while (it != hv[i].end() && *it <= rf) {
                    del.push_back(*it);
                    used[i][*it] = 1;
                    bfs.emplace(i, *it);
                    it++;
                }
                for (auto j : del) {
                    hv[i].erase(j);
                }
            }
        }
        vector<pair<int, int>> nw;
        while (!bfs.empty()) {
            auto [i, j] = bfs.front();
            bfs.pop();
            nw.emplace_back(i, j);
            for (auto [fx, fy] : cord) {
                int x = i + fx, y = j + fy;
                if (x < 0 || y < 0 || x == r || y == c || a[x][y] == '#') {
                    continue;
                }
                if (used[x][y]) {
                    continue;
                }
                used[x][y] = 1;
                hv[x].erase(y);
                bfs.emplace(x, y);
            }
        }
        elems = nw;
        col++;
    }
    cout << col;
}
#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...