結果

問題 No.3679 なんかでっかい虫リターンズ
コンテスト
ユーザー Mikan04y
提出日時 2026-07-10 18:29:00
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3 ms / 2,000 ms
+ 689µs
コード長 2,084 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,442 ms
コンパイル使用メモリ 364,052 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-09-05 12:33:20
合計ジャッジ時間 3,845 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define all(x) x.begin(), x.end()

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};

int H, W;

bool in(int x, int y) {
    return (0 <= x && x < H && 0 <= y && y < W);
}

template <typename T>
inline bool chmin(T& a, const T& b) {
    return ((a > b) ? (a = b, true) : (false));
}

void solve() {
    cin >> H >> W;
    int A, B;
    cin >> A >> B;
    A--, B--;
    int R1, C1, R2, C2;
    cin >> R1 >> C1 >> R2 >> C2;
    R1--, C1--, R2--, C2--;
    int P, Q;
    cin >> P >> Q;
    P--, Q--;

    vector grid(H, vector<int>(W, -1));
    using Tuple = tuple<int, int, int>;
    queue<Tuple> que;
    que.push(Tuple(0, A, B));
    while (!que.empty()) {
        auto [cnt, x, y] = que.front();
        que.pop();
        if (grid[x][y] != -1) continue;
        grid[x][y] = cnt;
        rep(i, 0, 4) {
            int nx = x + dx[i], ny = y + dy[i];
            if (!in(nx, ny)) continue;
            que.push(Tuple(cnt + 1, nx, ny));
        }
    }

    int mincnt = 1e9;
    int retcnt = grid[P][Q];
    rep(i, R1, R2 + 1) {
        rep(j, C1, C2 + 1) {
            chmin(mincnt, grid[i][j]);
        }
    }
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (R1 <= i && i <= R2 && C1 <= j && j <= C2 && mincnt == grid[i][j]) {
                que.push(Tuple(mincnt, i, j));
            }
            grid[i][j] = -1;
        }
    }

    while (!que.empty()) {
        auto [cnt, x, y] = que.front();
        que.pop();
        if (grid[x][y] != -1) continue;
        grid[x][y] = cnt;
        if (x == P && y == Q) break;
        rep(i, 0, 4) {
            int nx = x + dx[i], ny = y + dy[i];
            if (!in(nx, ny)) continue;
            que.push(Tuple(cnt + 1, nx, ny));
        }
    }

    int ans = grid[P][Q] + retcnt;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
}
0