結果

問題 No.1916 Making Palindrome on Gird
ユーザー tassei903tassei903
提出日時 2022-04-29 22:53:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 557 ms / 3,000 ms
コード長 2,352 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 86,320 KB
実行使用メモリ 144,328 KB
最終ジャッジ日時 2023-09-11 14:19:19
合計ジャッジ時間 9,921 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,636 KB
testcase_01 AC 73 ms
70,800 KB
testcase_02 AC 94 ms
75,756 KB
testcase_03 AC 78 ms
70,752 KB
testcase_04 AC 80 ms
75,312 KB
testcase_05 AC 80 ms
75,452 KB
testcase_06 AC 81 ms
75,740 KB
testcase_07 AC 80 ms
75,296 KB
testcase_08 AC 82 ms
75,472 KB
testcase_09 AC 168 ms
102,656 KB
testcase_10 AC 77 ms
70,624 KB
testcase_11 AC 87 ms
76,416 KB
testcase_12 AC 80 ms
75,556 KB
testcase_13 AC 223 ms
109,936 KB
testcase_14 AC 147 ms
91,708 KB
testcase_15 AC 268 ms
111,516 KB
testcase_16 AC 293 ms
124,944 KB
testcase_17 AC 337 ms
139,132 KB
testcase_18 AC 423 ms
144,272 KB
testcase_19 AC 418 ms
143,820 KB
testcase_20 AC 414 ms
144,220 KB
testcase_21 AC 421 ms
144,328 KB
testcase_22 AC 417 ms
143,860 KB
testcase_23 AC 79 ms
70,764 KB
testcase_24 AC 76 ms
70,656 KB
testcase_25 AC 78 ms
70,484 KB
testcase_26 AC 75 ms
70,552 KB
testcase_27 AC 75 ms
70,668 KB
testcase_28 AC 548 ms
143,016 KB
testcase_29 AC 557 ms
143,172 KB
testcase_30 AC 545 ms
143,084 KB
testcase_31 AC 538 ms
142,860 KB
testcase_32 AC 551 ms
143,060 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