結果

問題 No.1916 Making Palindrome on Gird
ユーザー tamatotamato
提出日時 2022-04-29 22:21:24
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,310 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 94,320 KB
最終ジャッジ日時 2023-09-11 13:40:55
合計ジャッジ時間 7,759 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,264 KB
testcase_01 AC 69 ms
71,128 KB
testcase_02 AC 76 ms
76,124 KB
testcase_03 AC 68 ms
71,312 KB
testcase_04 RE -
testcase_05 AC 77 ms
76,324 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 67 ms
71,424 KB
testcase_10 AC 67 ms
71,404 KB
testcase_11 RE -
testcase_12 AC 69 ms
71,120 KB
testcase_13 AC 221 ms
81,824 KB
testcase_14 RE -
testcase_15 AC 228 ms
81,300 KB
testcase_16 AC 208 ms
81,224 KB
testcase_17 AC 322 ms
91,552 KB
testcase_18 AC 350 ms
94,040 KB
testcase_19 AC 345 ms
93,984 KB
testcase_20 AC 353 ms
94,024 KB
testcase_21 AC 349 ms
94,280 KB
testcase_22 AC 353 ms
94,316 KB
testcase_23 AC 68 ms
71,324 KB
testcase_24 AC 68 ms
71,504 KB
testcase_25 AC 73 ms
71,588 KB
testcase_26 AC 67 ms
71,368 KB
testcase_27 AC 66 ms
71,136 KB
testcase_28 AC 331 ms
94,008 KB
testcase_29 AC 341 ms
94,104 KB
testcase_30 AC 331 ms
94,032 KB
testcase_31 AC 333 ms
93,792 KB
testcase_32 AC 339 ms
94,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007


def main():
    import sys
    input = sys.stdin.readline

    H, W = map(int, input().split())
    G = []
    for _ in range(H):
        G.append(input().rstrip('\n'))

    dp = [[0] * W for _ in range(H)]
    if G[0][0] != G[-1][-1]:
        print(0)
        exit()
    dp[0][H-1] = 1
    for hw in range((H + W - 2) // 2):
        dp_new = [[0] * W for _ in range(H)]
        for hl in range(H):
            wl = hw - hl
            if wl < 0 or wl >= W:
                continue
            for hr in range(H):
                wr = H + W - 2 - hw - hr
                if wr < 0 or wr >= W:
                    continue
                if hl != H - 1:
                    hl_new = hl + 1
                    sl = G[hl_new][wl]
                    if hr != 0:
                        hr_new = hr - 1
                        sr = G[hr_new][wr]
                        if sl == sr:
                            dp_new[hl_new][hr_new] = (dp_new[hl_new][hr_new] + dp[hl][hr])%mod
                    if wr != 0:
                        wr_new = wr - 1
                        sr = G[hr][wr_new]
                        if sl == sr:
                            dp_new[hl_new][hr] = (dp_new[hl_new][hr] + dp[hl][hr]) % mod
                if wl != W - 1:
                    hl_new = hl
                    sl = G[hl_new][wl+1]
                    if hr != 0:
                        hr_new = hr - 1
                        sr = G[hr_new][wr]
                        if sl == sr:
                            dp_new[hl_new][hr_new] = (dp_new[hl_new][hr_new] + dp[hl][hr]) % mod
                    if wr != 0:
                        wr_new = wr - 1
                        sr = G[hr][wr_new]
                        if sl == sr:
                            dp_new[hl_new][hr] = (dp_new[hl_new][hr] + dp[hl][hr]) % mod
        dp = dp_new

    if (H + W) & 1:
        ans = 0
        for hl in range(H):
            wl = (H + W - 2) // 2 - hl
            for hr in range(H):
                wr = (H + W - 2) // 2 + 1 - hr
                if abs(hl - hr) + abs(wl - wr) == 1:
                    ans = (ans + dp[hl][hr])%mod
        print(ans)
    else:
        ans = 0
        for hl in range(H):
            ans = (ans + dp[hl][hl])%mod
        print(ans)


if __name__ == '__main__':
    main()
0