結果

問題 No.1916 Making Palindrome on Gird
ユーザー lilictakalilictaka
提出日時 2022-05-16 18:34:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,279 ms / 3,000 ms
コード長 2,037 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 142,020 KB
最終ジャッジ日時 2023-10-12 12:19:36
合計ジャッジ時間 23,612 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,196 KB
testcase_01 AC 73 ms
71,416 KB
testcase_02 AC 112 ms
77,636 KB
testcase_03 AC 73 ms
71,340 KB
testcase_04 AC 112 ms
77,488 KB
testcase_05 AC 108 ms
77,028 KB
testcase_06 AC 116 ms
77,508 KB
testcase_07 AC 111 ms
77,500 KB
testcase_08 AC 114 ms
77,592 KB
testcase_09 AC 73 ms
71,192 KB
testcase_10 AC 72 ms
71,424 KB
testcase_11 AC 112 ms
77,564 KB
testcase_12 AC 100 ms
76,916 KB
testcase_13 AC 514 ms
98,000 KB
testcase_14 AC 399 ms
93,248 KB
testcase_15 AC 649 ms
105,504 KB
testcase_16 AC 344 ms
90,436 KB
testcase_17 AC 1,123 ms
131,652 KB
testcase_18 AC 1,279 ms
141,488 KB
testcase_19 AC 1,203 ms
141,556 KB
testcase_20 AC 1,195 ms
141,828 KB
testcase_21 AC 1,196 ms
141,556 KB
testcase_22 AC 1,159 ms
141,412 KB
testcase_23 AC 1,117 ms
140,576 KB
testcase_24 AC 1,101 ms
140,552 KB
testcase_25 AC 1,103 ms
140,248 KB
testcase_26 AC 1,113 ms
141,764 KB
testcase_27 AC 1,052 ms
140,868 KB
testcase_28 AC 1,231 ms
141,756 KB
testcase_29 AC 1,224 ms
142,020 KB
testcase_30 AC 1,222 ms
141,768 KB
testcase_31 AC 1,222 ms
141,728 KB
testcase_32 AC 1,224 ms
141,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W = map(int,input().split())
board = []
for _ in range(H):
    tmp = list(input())
    board.append(tmp)
def convert(k,h1,h2):
    return (H**2) * k + H * h1 + h2
def ch1(k,h,mid):
    return (h,mid-k-h)
def ch2(k,h,mid):
    return (h,mid+k-h)
mod = 10 ** 9 + 7
def isin(h,w):
    if 0 <= h < H and 0 <= w < W:
        return True
    else:
        return False
def check(k,h1,h2,mid1,mid2):
    h1,w1 = ch1(k,h1,mid1)
    h2,w2 = ch2(k,h2,mid2)
    if isin(h1,w1) and isin(h2,w2) and board[h1][w1] == board[h2][w2]:
        return True
    else:
        return False
def solve():
    if (H + W ) % 2 == 0:
        mid = (H-1+W-1) //2
        dp = [0 for _ in range((H**2) * (mid+1))]
        for h1 in range(H):
            dp[convert(0,h1,h1)] = 1
        for k in range(mid):
            for h1 in range(H):
                for h2 in range(H):
                    for dh1,dh2 in [(-1,0),(-1,1),(0,0),(0,1)]:
                        nh1 = h1 + dh1
                        nh2 = h2 + dh2
                        if check(k+1,nh1,nh2,mid,mid):
                            dp[convert(k+1,nh1,nh2)] += dp[convert(k,h1,h2)]
                            dp[convert(k+1,nh1,nh2)] %= mod
        return dp[convert(mid,0,H-1)]
    else:
        mid1 = (H-1+W-1 -1) //2
        mid2 = (H-1+W-1 +1) //2
        dp = [0 for _ in range((H**2) * (mid1+1))]
        for h1 in range(H):
            if check(0,h1,h1,mid1,mid2):
                dp[convert(0,h1,h1)] += 1
            if check(0,h1,h1+1,mid1,mid2):
                dp[convert(0,h1,h1+1)] += 1
        for k in range(mid1):
            for h1 in range(H):
                for h2 in range(H):
                    for dh1,dh2 in [(-1,0),(-1,1),(0,0),(0,1)]:
                        nh1 = h1 + dh1
                        nh2 = h2 + dh2
                        if check(k+1,nh1,nh2,mid1,mid2):
                            dp[convert(k+1,nh1,nh2)] += dp[convert(k,h1,h2)]
                            dp[convert(k+1,nh1,nh2)] %= mod
        return dp[(convert(mid1,0,H-1))]
print(solve())


0