結果

問題 No.2928 Gridpath
ユーザー hanage
提出日時 2024-10-12 16:20:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,185 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 2,132 ms
コンパイル使用メモリ 198,852 KB
最終ジャッジ日時 2025-02-24 18:31:34
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int H, W;
int Si, Sj;
int Gi, Gj;

vector<vector<int>> grid;

vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, -1, 0, 1};
int ans = 0;

// rinsetu sitete saga 1 yori ookii mono ga attara toomawari siteru?
void dfs(int cx, int cy, int t) {
    if (cx == Gi && cy == Gj) {
        bool ok = true;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for (int k = 0; k < 4; k++) {
                    int i2 = i + dx[k];
                    int j2 = j + dy[k];
                    if (i2 < 0 || H <= i2) continue;
                    if (j2 < 0 || W <= j2) continue;
                    if (grid[i][j] == -1) continue;
                    if (grid[i2][j2] == -1) continue;
                    if (abs(grid[i][j] - grid[i2][j2]) > 1) ok = false;
                }
            }
        }
        if (ok) ans++;
        return;
        /**
        bool ok = true;
        for (int i = 0; i < H - 1; i++) {
            for (int j = 0; j < W - 1; j++) {
                if (grid[i][j] == '#' && grid[i][j + 1] == '#' && grid[i + 1][j] == '#' && grid[i + 1][j + 1] == '#') {
                    ok = false;
                }
            }
        }
        if (ok) ans++;
        return;
        **/
    }
    for (int i = 0; i < 4; i++) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if (nx < 0 || H <= nx) continue;
        if (ny < 0 || W <= ny) continue;
        if (grid[nx][ny] == -1) {
            grid[nx][ny] = t;
            dfs(nx, ny, t + 1);
            grid[nx][ny] = -1;
        }
    }
}

int main() {
    cin >> H >> W;
    cin >> Si >> Sj;
    cin >> Gi >> Gj;
    Si--, Sj--, Gi--, Gj--;
    grid.assign(H, vector<int>(W, -1));
    grid[Si][Sj] = 0;
    dfs(Si, Sj, 1);
    cout << ans << endl;
    return 0;
}
0