結果

問題 No.3429 Palindromic Path (Hard)
コンテスト
ユーザー norioc
提出日時 2026-11-06 03:29:46
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 798 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 81 ms
コンパイル使用メモリ 64,476 KB
実行使用メモリ 420,740 KB
最終ジャッジ日時 2026-11-06 03:29:52
合計ジャッジ時間 6,187 ms
ジャッジサーバーID
(参考情報)
judge7_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from itertools import product
from functools import cache

MOD = 998244353
N = int(input())
S = []
for i in range(N):
    S.append(input())


@cache
def f(r1, c1, r2, c2) -> int:
    if r1 == r2 and c1 == c2:
        return 1

    if r1 == r2 and c1+1 == c2:
        return 1 if S[r1][c1] == S[r2][c2] else 0

    if r1+1 == r2 and c1 == c2:
        return 1 if S[r1][c1] == S[r2][c2] else 0

    if S[r1][c1] != S[r2][c2]:
        return 0

    res = 0
    for (dr1, dc1), (dr2, dc2) in product([(0, 1), (1, 0)], [(0, -1), (-1, 0)]):
        nr1 = r1 + dr1
        nc1 = c1 + dc1
        nr2 = r2 + dr2
        nc2 = c2 + dc2
        if nr1 > nr2: continue
        if nc1 > nc2: continue
        res += f(nr1, nc1, nr2, nc2)
        res %= MOD

    return res


ans = f(0, 0, N-1, N-1)
print(ans)
0