結果

問題 No.2928 Gridpath
ユーザー detteiuu
提出日時 2024-10-12 16:04:55
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 1,738 bytes
コンパイル時間 5,229 ms
コンパイル使用メモリ 319,660 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-12 16:05:07
合計ジャッジ時間 9,243 ms
ジャッジサーバーID
(参考情報)
judge / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define pass (void)0
#define INF (1<<30)-1
#define INFLL (1LL<<60)-1
using namespace std;
using namespace atcoder;
using mint = modint998244353;
using ll = long long;

int H, W, sh, sw, gh, gw, ans = 0;
vector<vector<int>> visited;
vector<pair<int, int>> direction = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

bool judge() {
    deque<pair<int, int>> que;
    que.push_back({gh, gw});
    vector<vector<int>> dist(H, vector<int>(W, -1));
    dist[gh][gw] = visited[gh][gw];
    
    while (!que.empty()) {
        auto [h, w] = que.front();
        que.pop_front();
        if (h == sh && w == sw) return true;
        
        for (auto [dh, dw] : direction) {
            int nh = h + dh, nw = w + dw;
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (visited[nh][nw] != -1 && visited[h][w] - 1 > visited[nh][nw]) return false;
            if (visited[nh][nw] != -1 && dist[nh][nw] == -1) {
                dist[nh][nw] = dist[h][w] - 1;
                que.push_back({nh, nw});
            }
        }
    }
    return false;
}

void dfs(int h, int w, int cnt) {
    visited[h][w] = cnt;
    cnt++;
    
    if (h == gh && w == gw) {
        if (judge()) ans++;
        visited[h][w] = -1;
        return;
    }
    
    for (auto [dh, dw] : direction) {
        int nh = h + dh, nw = w + dw;
        if (nh >= 0 && nh < H && nw >= 0 && nw < W && visited[nh][nw] == -1) {
            dfs(nh, nw, cnt);
        }
    }
    visited[h][w] = -1;
}

int main() {
    cin >> H >> W;
    cin >> sh >> sw;
    cin >> gh >> gw;
    sh--, sw--, gh--, gw--;
    
    visited.resize(H, vector<int>(W, -1));
    dfs(sh, sw, 0);
    
    cout << ans << endl;
    return 0;
}
0