結果
問題 | No.2928 Gridpath |
ユーザー |
![]() |
提出日時 | 2024-10-12 16:31:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 133 ms / 2,000 ms |
コード長 | 986 bytes |
コンパイル時間 | 319 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 77,036 KB |
最終ジャッジ日時 | 2024-10-12 16:31:03 |
合計ジャッジ時間 | 2,773 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 |
ソースコード
h, w = map(int, input().split()) si, sj = map(lambda x: int(x)-1, input().split()) gi, gj = map(lambda x: int(x)-1, input().split()) ans = 0 grid = [[False]*w for _ in range(h)] grid[si][sj] = True stack = [[0, si, sj]] d = [(-1, 0), (0, -1), (1, 0), (0, 1)] while stack: _, i, j = stack[-1] if i==gi and j==gj: ans += 1 stack[-1][0] = 4 if stack[-1][0]==4: stack.pop() grid[i][j] = False continue while stack and stack[-1][0] < 4: di, dj = d[stack[-1][0]] ni = i+di nj = j+dj stack[-1][0] += 1 if 0 <= ni < h and 0 <= nj < w and (not grid[ni][nj]): pass else: continue cnt = 0 for y in range(4): ddi, ddj = d[y] if 0 <= ni+ddi < h and 0 <= nj+ddj < w: cnt += grid[ni+ddi][nj+ddj] if cnt == 1: stack.append([0, ni, nj]) grid[ni][nj] = True break print(ans)