結果

問題 No.1916 Making Palindrome on Gird
ユーザー tassei903tassei903
提出日時 2022-04-29 22:53:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 554 ms / 3,000 ms
コード長 2,352 bytes
コンパイル時間 1,865 ms
コンパイル使用メモリ 82,044 KB
実行使用メモリ 143,684 KB
最終ジャッジ日時 2024-06-29 04:31:37
合計ジャッジ時間 8,238 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,708 KB
testcase_01 AC 36 ms
52,684 KB
testcase_02 AC 52 ms
64,924 KB
testcase_03 AC 38 ms
53,120 KB
testcase_04 AC 45 ms
60,144 KB
testcase_05 AC 47 ms
61,424 KB
testcase_06 AC 46 ms
61,620 KB
testcase_07 AC 44 ms
60,828 KB
testcase_08 AC 46 ms
60,956 KB
testcase_09 AC 130 ms
103,368 KB
testcase_10 AC 37 ms
53,556 KB
testcase_11 AC 51 ms
64,412 KB
testcase_12 AC 44 ms
59,764 KB
testcase_13 AC 186 ms
108,872 KB
testcase_14 AC 104 ms
85,316 KB
testcase_15 AC 230 ms
111,536 KB
testcase_16 AC 250 ms
124,544 KB
testcase_17 AC 301 ms
138,444 KB
testcase_18 AC 389 ms
143,448 KB
testcase_19 AC 384 ms
143,684 KB
testcase_20 AC 380 ms
143,120 KB
testcase_21 AC 382 ms
143,088 KB
testcase_22 AC 378 ms
143,580 KB
testcase_23 AC 40 ms
53,424 KB
testcase_24 AC 40 ms
53,632 KB
testcase_25 AC 41 ms
54,532 KB
testcase_26 AC 41 ms
55,072 KB
testcase_27 AC 40 ms
54,940 KB
testcase_28 AC 527 ms
143,136 KB
testcase_29 AC 554 ms
143,260 KB
testcase_30 AC 527 ms
142,456 KB
testcase_31 AC 526 ms
142,336 KB
testcase_32 AC 536 ms
142,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w = map(int, input().split())

s = [input() for i in range(h)]
if s[0][0] != s[-1][-1]:
    print(0)
    exit()
n = (h+w)//2
dp = [[[0 for k in range(w)]for j in range(w)]for i in range(n)]
dp[0][0][w-1] = 1
mod = 10**9 + 7

for i in range(n-1):
    for j in range(w):
        for k in range(w):
            now = dp[i][j][k]
            if now == 0:
                continue
            #print(i,j,k)
            ni = i + 1
            nj = j
            nk = k - 1
            nx1 = ni-nj
            ny1 = nj
            nx2 = h+w-2-ni-nk
            ny2 = nk
            #print(nx1,ny1,nx2,ny2)
            if 0 <= nx1 < h and 0 <= ny1 < w and 0 <= nx2 < h and 0 <= ny2 < w and s[nx1][ny1] == s[nx2][ny2]:
                dp[ni][nj][nk] += now
                dp[ni][nj][nk] %= mod
            ni = i + 1
            nj = j
            nk = k
            nx1 = ni-nj
            ny1 = nj
            nx2 = h+w-2-ni-nk
            ny2 = nk
            if 0 <= nx1 < h and 0 <= ny1 < w and 0 <= nx2 < h and 0 <= ny2 < w and s[nx1][ny1] == s[nx2][ny2]:
                dp[ni][nj][nk] += now
                dp[ni][nj][nk] %= mod
            ni = i + 1
            nj = j + 1
            nk = k - 1
            nx1 = ni-nj
            ny1 = nj
            nx2 = h+w-2-ni-nk
            ny2 = nk
            #print(nx1,ny1,nx2,ny2)
            if 0 <= nx1 < h and 0 <= ny1 < w and 0 <= nx2 < h and 0 <= ny2 < w and s[nx1][ny1] == s[nx2][ny2]:
                dp[ni][nj][nk] += now
                dp[ni][nj][nk] %= mod
            ni = i + 1
            nj = j + 1
            nk = k
            nx1 = ni-nj
            ny1 = nj
            nx2 = h+w-2-ni-nk
            ny2 = nk
            if 0 <= nx1 < h and 0 <= ny1 < w and 0 <= nx2 < h and 0 <= ny2 < w and s[nx1][ny1] == s[nx2][ny2]:
                dp[ni][nj][nk] += now
                dp[ni][nj][nk] %= mod
if (h+w-1)%2:
    ans = 0
    for j in range(w):
        ans += dp[n-1][j][j]
        ans %= mod
    print(ans)
else:
    ans = 0
    ni = n-1
    for nj in range(w):
        for nk in range(w):
            nx1 = ni-nj
            ny1 = nj
            nx2 = h+w-2-ni-nk
            ny2 = nk
            if 0 <= nx1 < h and 0 <= ny1 < w and 0 <= nx2 < h and 0 <= ny2 < w and abs(nx1-nx2)+abs(ny1-ny2)<=1:
                ans += dp[ni][nj][nk]
                ans %= mod
    print(ans)
0