結果

問題 No.1916 Making Palindrome on Gird
コンテスト
ユーザー norioc
提出日時 2026-07-29 12:01:28
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 2,344 ms / 3,000 ms
+ 696µs
コード長 866 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 301 ms
コンパイル使用メモリ 95,600 KB
実行使用メモリ 464,896 KB
最終ジャッジ日時 2026-07-29 12:02:13
合計ジャッジ時間 23,696 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from itertools import product
from functools import cache

MOD = 10**9 + 7
H, W = map(int, input().split())
S = []
for i in range(H):
    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


if S[0][0] != S[H-1][W-1]:
    print(0)
    exit()

ans = f(0, 0, H-1, W-1)
print(ans)
0