結果

問題 No.1916 Making Palindrome on Gird
ユーザー 👑 tamatotamato
提出日時 2022-04-29 22:24:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 355 ms / 3,000 ms
コード長 2,366 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 94,176 KB
最終ジャッジ日時 2023-09-11 13:45:02
合計ジャッジ時間 7,733 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,188 KB
testcase_01 AC 72 ms
71,224 KB
testcase_02 AC 83 ms
76,296 KB
testcase_03 AC 74 ms
71,320 KB
testcase_04 AC 87 ms
76,088 KB
testcase_05 AC 84 ms
76,360 KB
testcase_06 AC 84 ms
76,324 KB
testcase_07 AC 82 ms
76,060 KB
testcase_08 AC 89 ms
76,252 KB
testcase_09 AC 75 ms
71,348 KB
testcase_10 AC 72 ms
71,164 KB
testcase_11 AC 82 ms
75,732 KB
testcase_12 AC 75 ms
71,268 KB
testcase_13 AC 224 ms
80,792 KB
testcase_14 AC 181 ms
79,464 KB
testcase_15 AC 233 ms
80,916 KB
testcase_16 AC 211 ms
79,496 KB
testcase_17 AC 330 ms
90,220 KB
testcase_18 AC 355 ms
93,948 KB
testcase_19 AC 345 ms
93,828 KB
testcase_20 AC 354 ms
94,168 KB
testcase_21 AC 355 ms
94,176 KB
testcase_22 AC 354 ms
94,032 KB
testcase_23 AC 73 ms
71,168 KB
testcase_24 AC 74 ms
71,400 KB
testcase_25 AC 73 ms
71,148 KB
testcase_26 AC 75 ms
71,240 KB
testcase_27 AC 74 ms
71,496 KB
testcase_28 AC 342 ms
93,744 KB
testcase_29 AC 342 ms
93,964 KB
testcase_30 AC 332 ms
93,908 KB
testcase_31 AC 338 ms
93,760 KB
testcase_32 AC 340 ms
94,124 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] * H 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] * H 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:
                    if 0 <= wl < W and 0 <= wr < W:
                        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