제출 #1342715

#제출 시각아이디문제언어결과실행 시간메모리
1342715po_rag526Toy (CEOI24_toy)C++17
35 / 100
1601 ms241172 KiB
#include <bits/stdc++.h>
using namespace std;

struct State {
    int xh, yh, xv, yv;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int W, H, K, L;
    cin >> W >> H >> K >> L;

    int xh0, yh0, xv0, yv0;
    cin >> xh0 >> yh0 >> xv0 >> yv0;

    vector<string> g(H);
    for (int i = 0; i < H; i++) cin >> g[i];

    int tx = -1, ty = -1;
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            if (g[y][x] == '*') {
                tx = x;
                ty = y;
            }
        }
    }

    // Row prefix sums for obstacles
    vector<vector<int>> rowPref(H, vector<int>(W + 1, 0));
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            rowPref[y][x + 1] = rowPref[y][x] + (g[y][x] == 'X');
        }
    }

    // Column prefix sums for obstacles
    vector<vector<int>> colPref(W, vector<int>(H + 1, 0));
    for (int x = 0; x < W; x++) {
        for (int y = 0; y < H; y++) {
            colPref[x][y + 1] = colPref[x][y] + (g[y][x] == 'X');
        }
    }

    auto horiz_ok = [&](int xh, int yh) -> bool {
        if (yh < 0 || yh >= H) return false;
        if (xh < 0 || xh + K > W) return false;
        return (rowPref[yh][xh + K] - rowPref[yh][xh] == 0);
    };

    auto vert_ok = [&](int xv, int yv) -> bool {
        if (xv < 0 || xv >= W) return false;
        if (yv < 0 || yv + L > H) return false;
        return (colPref[xv][yv + L] - colPref[xv][yv] == 0);
    };

    auto valid = [&](int xh, int yh, int xv, int yv) -> bool {
        if (!horiz_ok(xh, yh)) return false;
        if (!vert_ok(xv, yv)) return false;

        // Must overlap in exactly one cell
        if (!(xh <= xv && xv < xh + K)) return false;
        if (!(yv <= yh && yh < yv + L)) return false;

        return true;
    };

    if (!valid(xh0, yh0, xv0, yv0)) {
        cout << "NO\n";
        return 0;
    }

    // Since constraints are not shown in the screenshot,
    // we use hash-based visited for the state space.
    auto encode = [&](int xh, int yh, int xv, int yv) -> unsigned long long {
        unsigned long long a = (unsigned long long)xh;
        unsigned long long b = (unsigned long long)yh;
        unsigned long long c = (unsigned long long)xv;
        unsigned long long d = (unsigned long long)yv;
        return (((a * 3001ULL + b) * 3001ULL + c) * 3001ULL + d);
    };

    queue<State> q;
    unordered_set<unsigned long long> vis;
    vis.reserve(1 << 20);

    q.push({xh0, yh0, xv0, yv0});
    vis.insert(encode(xh0, yh0, xv0, yv0));

    int dxh[8] = {-1, 1, 0, 0, 0, 0, 0, 0};
    int dyh[8] = {0, 0, -1, 1, 0, 0, 0, 0};
    int dxv[8] = {0, 0, 0, 0, -1, 1, 0, 0};
    int dyv[8] = {0, 0, 0, 0, 0, 0, -1, 1};

    while (!q.empty()) {
        State cur = q.front();
        q.pop();

        int overlapX = cur.xv;
        int overlapY = cur.yh;

        if (overlapX == tx && overlapY == ty) {
            cout << "YES\n";
            return 0;
        }

        for (int dir = 0; dir < 8; dir++) {
            int nxh = cur.xh + dxh[dir];
            int nyh = cur.yh + dyh[dir];
            int nxv = cur.xv + dxv[dir];
            int nyv = cur.yv + dyv[dir];

            if (!valid(nxh, nyh, nxv, nyv)) continue;

            auto code = encode(nxh, nyh, nxv, nyv);
            if (vis.insert(code).second) {
                q.push({nxh, nyh, nxv, nyv});
            }
        }
    }

    cout << "NO\n";
    return 0;
}
#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...