結果
問題 | No.2928 Gridpath |
ユーザー | miya145592 |
提出日時 | 2024-10-12 16:05:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 170 ms / 2,000 ms |
コード長 | 1,795 bytes |
コンパイル時間 | 134 ms |
コンパイル使用メモリ | 82,276 KB |
実行使用メモリ | 78,264 KB |
最終ジャッジ日時 | 2024-10-12 16:05:49 |
合計ジャッジ時間 | 2,731 ms |
ジャッジサーバーID (参考情報) |
judge / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,564 KB |
testcase_01 | AC | 43 ms
55,328 KB |
testcase_02 | AC | 170 ms
77,872 KB |
testcase_03 | AC | 38 ms
54,840 KB |
testcase_04 | AC | 44 ms
54,180 KB |
testcase_05 | AC | 51 ms
64,640 KB |
testcase_06 | AC | 96 ms
76,996 KB |
testcase_07 | AC | 42 ms
60,528 KB |
testcase_08 | AC | 39 ms
55,316 KB |
testcase_09 | AC | 93 ms
76,924 KB |
testcase_10 | AC | 51 ms
64,540 KB |
testcase_11 | AC | 40 ms
55,184 KB |
testcase_12 | AC | 40 ms
54,300 KB |
testcase_13 | AC | 40 ms
54,592 KB |
testcase_14 | AC | 41 ms
56,036 KB |
testcase_15 | AC | 90 ms
76,204 KB |
testcase_16 | AC | 148 ms
78,264 KB |
testcase_17 | AC | 117 ms
77,412 KB |
testcase_18 | AC | 153 ms
77,340 KB |
testcase_19 | AC | 140 ms
77,460 KB |
testcase_20 | AC | 37 ms
55,508 KB |
testcase_21 | AC | 136 ms
77,372 KB |
testcase_22 | AC | 140 ms
77,780 KB |
ソースコード
from collections import deque def check(order): field = [[1 for _ in range(W)] for _ in range(H)] for o in order: i, j = o field[i][j] = 0 global si, sj global gi, gj deq = deque() deq.append((si, sj)) dist = [[INF for _ in range(W)] for _ in range(H)] dist[si][sj] = 0 while deq: i, j = deq.popleft() for di, dj in dir: ni = i+di nj = j+dj if 0<=ni<H and 0<=nj<W and field[ni][nj]==0: if dist[ni][nj]>dist[i][j]+1: dist[ni][nj] = dist[i][j]+1 deq.append((ni, nj)) if len(order)-1==dist[gi][gj]: return True else: return False def dfs(i, j, order): global gi, gj if i==gi and j==gj: if check(order): ans.add(tuple(order)) return for di, dj in dir: ni = i+di nj = j+dj if 0<=ni<H and 0<=nj<W and seen[ni][nj]==0: tmp = 0 for ddi, ddj in dir: nni = ni+ddi nnj = nj+ddj if 0<=nni<H and 0<=nnj<W: if (nni, nnj) in order: tmp+=1 if tmp<=1: order.append((ni, nj)) seen[ni][nj] = 1 dfs(ni, nj, order) order.pop() seen[ni][nj] = 0 INF = 10**9 import sys import pypyjit pypyjit.set_param('max_unroll_recursion=-1') input = sys.stdin.readline H, W = map(int, input().split()) si, sj = map(int, input().split()) gi, gj = map(int, input().split()) si-=1 sj-=1 gi-=1 gj-=1 dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] seen = [[0 for _ in range(W)] for _ in range(H)] seen[si][sj] = 1 ans = set() order = [(si, sj)] dfs(si, sj, order) print(len(ans))