結果

問題 No.1916 Making Palindrome on Gird
ユーザー roarisroaris
提出日時 2022-05-14 19:37:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,290 ms / 3,000 ms
コード長 1,542 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 87,312 KB
実行使用メモリ 149,712 KB
最終ジャッジ日時 2023-09-30 12:10:55
合計ジャッジ時間 20,383 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
77,216 KB
testcase_01 AC 92 ms
71,552 KB
testcase_02 AC 132 ms
79,092 KB
testcase_03 AC 99 ms
77,168 KB
testcase_04 AC 126 ms
78,528 KB
testcase_05 AC 129 ms
78,376 KB
testcase_06 AC 125 ms
78,032 KB
testcase_07 AC 130 ms
79,140 KB
testcase_08 AC 125 ms
78,936 KB
testcase_09 AC 97 ms
77,312 KB
testcase_10 AC 89 ms
71,892 KB
testcase_11 AC 119 ms
78,332 KB
testcase_12 AC 114 ms
77,888 KB
testcase_13 AC 554 ms
116,188 KB
testcase_14 AC 401 ms
105,364 KB
testcase_15 AC 687 ms
121,612 KB
testcase_16 AC 538 ms
114,436 KB
testcase_17 AC 1,096 ms
144,224 KB
testcase_18 AC 1,255 ms
149,712 KB
testcase_19 AC 1,290 ms
149,504 KB
testcase_20 AC 1,270 ms
149,440 KB
testcase_21 AC 1,275 ms
149,672 KB
testcase_22 AC 1,259 ms
149,420 KB
testcase_23 AC 91 ms
71,812 KB
testcase_24 AC 93 ms
71,720 KB
testcase_25 AC 94 ms
71,652 KB
testcase_26 AC 92 ms
71,740 KB
testcase_27 AC 92 ms
71,448 KB
testcase_28 AC 1,285 ms
149,584 KB
testcase_29 AC 1,282 ms
149,576 KB
testcase_30 AC 1,289 ms
149,628 KB
testcase_31 AC 1,271 ms
149,456 KB
testcase_32 AC 1,280 ms
149,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

H, W = map(int, input().split())
S = [input()[:-1] for _ in range(H)]

if S[0][0]!=S[-1][-1]:
    exit(print(0))

ind = [[-1]*W for _ in range(H)]
d = defaultdict(list)

for i in range(H):
    for j in range(W):
        ind[i][j] = len(d[H-1-i+W-1-j])
        d[H-1-i+W-1-j].append((i, j))

dp = [[[0]*210 for _ in range(W)] for _ in range(H)]
dp[0][0][0] = 1
MOD = 10**9+7

for i in range(H):
    for j in range(W):
        for k in range(210):
            if k>=len(d[i+j]):
                continue
            
            i2, j2 = d[i+j][k]
            
            if i>=i2 and j>=j2:
                continue
            
            for ni, nj in [(i+1, j), (i, j+1)]:
                for ni2, nj2 in [(i2-1, j2), (i2, j2-1)]:
                    if not (0<=ni<H and 0<=nj<W and 0<=ni2<H and 0<=nj2<W):
                        continue
                    
                    if (ni, nj)==(ni2, nj2) or S[ni][nj]==S[ni2][nj2]:
                        dp[ni][nj][ind[ni2][nj2]] += dp[i][j][k]
                        dp[ni][nj][ind[ni2][nj2]] %= MOD

ans = 0

for i in range(H):
    for j in range(W):
        if 2*(i+j)==H+W-2:
            ans += dp[i][j][ind[i][j]]
            ans %= MOD
            
        for ni, nj in [(i+1, j), (i, j+1)]:
            if not (0<=ni<H and 0<=nj<W):
                continue
            
            if i+j==H-ni+W-nj-2 and i+j+H-ni+W-nj==H+W-1:
                ans += dp[i][j][ind[ni][nj]]
                ans %= MOD

print(ans)
0