結果

問題 No.2928 Gridpath
コンテスト
ユーザー i_taku
提出日時 2025-10-17 11:56:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 829 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 77,764 KB
最終ジャッジ日時 2025-10-17 11:56:53
合計ジャッジ時間 3,610 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

H, W = map(int, input().split())
Si, Sj = map(int, input().split())
Si, Sj = Si - 1, Sj - 1
Gi, Gj = map(int, input().split())
Gi, Gj = Gi - 1, Gj - 1
vis = [[False] * W for _ in range(H)]
ans = 0
D = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def dfs(i, j):
    if (i, j) == (Gi, Gj):
        global ans
        ans += 1
    vis[i][j] = True
    for di, dj in D:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and not vis[ni][nj]:
            ok = True
            for di, dj in D:
                nni, nnj = ni + di, nj + dj
                if (nni, nnj) == (i, j):
                    continue
                if 0 <= nni < H and 0 <= nnj < W and vis[nni][nnj]:
                    ok = False
                    break
            if ok:
                dfs(ni, nj)
    vis[i][j] = False
dfs(Si, Sj)
print(ans)
0