結果

問題 No.1916 Making Palindrome on Gird
ユーザー sotanishysotanishy
提出日時 2022-04-29 21:45:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,626 ms / 3,000 ms
コード長 1,048 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 265,992 KB
最終ジャッジ日時 2023-09-11 12:44:02
合計ジャッジ時間 22,621 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,724 KB
testcase_01 AC 97 ms
71,596 KB
testcase_02 AC 120 ms
78,128 KB
testcase_03 AC 96 ms
71,436 KB
testcase_04 AC 96 ms
71,604 KB
testcase_05 AC 100 ms
76,232 KB
testcase_06 AC 101 ms
76,264 KB
testcase_07 AC 99 ms
76,284 KB
testcase_08 AC 102 ms
76,532 KB
testcase_09 AC 100 ms
76,212 KB
testcase_10 AC 94 ms
71,928 KB
testcase_11 AC 101 ms
76,328 KB
testcase_12 AC 99 ms
76,236 KB
testcase_13 AC 118 ms
77,932 KB
testcase_14 AC 117 ms
78,068 KB
testcase_15 AC 233 ms
82,296 KB
testcase_16 AC 187 ms
79,412 KB
testcase_17 AC 353 ms
91,592 KB
testcase_18 AC 945 ms
170,180 KB
testcase_19 AC 935 ms
164,828 KB
testcase_20 AC 956 ms
166,880 KB
testcase_21 AC 987 ms
168,680 KB
testcase_22 AC 980 ms
168,216 KB
testcase_23 AC 96 ms
71,700 KB
testcase_24 AC 96 ms
71,552 KB
testcase_25 AC 96 ms
71,564 KB
testcase_26 AC 98 ms
71,596 KB
testcase_27 AC 97 ms
71,644 KB
testcase_28 AC 2,610 ms
265,992 KB
testcase_29 AC 2,618 ms
264,456 KB
testcase_30 AC 2,626 ms
264,584 KB
testcase_31 AC 2,589 ms
264,592 KB
testcase_32 AC 2,593 ms
265,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
input = sys.stdin.readline

mod = 10**9 + 7
H, W = map(int, input().split())
S = [input().rstrip() for _ in range(H)]
if S[0][0] != S[H-1][W-1]:
    print(0)
    exit()
dp = {(0, 0, H-1, W-1): 1}
for k in range((H+W-2)//2):
    ndp = defaultdict(int)
    for (i, j, k, l), w in dp.items():
        if i > k or j > l:
            continue
        w %= mod
        for di, dj in [(1, 0), (0, 1)]:
            ni, nj = i+di, j+dj
            if H <= ni or W <= nj:
                continue
            for dk, dl in [(-1, 0), (0, -1)]:
                nk, nl = k+dk, l+dl
                if nk < 0 or nl < 0:
                    continue
                if S[ni][nj] == S[nk][nl]:
                    ndp[(ni, nj, nk, nl)] += w
    dp = ndp

ans = 0
if (H+W-1)%2:
    for (i, j, k, l), w in dp.items():
        if (i, j) == (k, l):
            ans = (ans + w) % mod
else:
    for (i, j, k, l), w in dp.items():
        if i <= k and j <= l and k-i+l-j == 1:
            ans = (ans + w) % mod
print(ans)

0