結果

問題 No.1916 Making Palindrome on Gird
ユーザー mkawa2mkawa2
提出日時 2022-04-29 22:47:00
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,834 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 86,856 KB
実行使用メモリ 268,424 KB
最終ジャッジ日時 2023-09-11 14:11:10
合計ジャッジ時間 17,287 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,412 KB
testcase_01 AC 90 ms
71,620 KB
testcase_02 AC 113 ms
77,840 KB
testcase_03 AC 86 ms
71,412 KB
testcase_04 AC 90 ms
76,264 KB
testcase_05 AC 88 ms
71,712 KB
testcase_06 AC 92 ms
76,196 KB
testcase_07 AC 95 ms
76,268 KB
testcase_08 AC 95 ms
76,248 KB
testcase_09 AC 94 ms
76,136 KB
testcase_10 AC 93 ms
71,788 KB
testcase_11 AC 92 ms
76,028 KB
testcase_12 AC 91 ms
76,104 KB
testcase_13 AC 123 ms
78,516 KB
testcase_14 AC 120 ms
78,588 KB
testcase_15 AC 365 ms
86,668 KB
testcase_16 AC 251 ms
80,744 KB
testcase_17 AC 630 ms
107,304 KB
testcase_18 AC 1,637 ms
196,724 KB
testcase_19 AC 1,587 ms
192,988 KB
testcase_20 AC 1,599 ms
194,456 KB
testcase_21 AC 1,645 ms
196,024 KB
testcase_22 AC 1,628 ms
192,780 KB
testcase_23 AC 84 ms
71,496 KB
testcase_24 AC 85 ms
71,496 KB
testcase_25 AC 85 ms
71,540 KB
testcase_26 AC 86 ms
71,792 KB
testcase_27 AC 86 ms
71,644 KB
testcase_28 TLE -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

from collections import defaultdict

h, w = LI()
n = h+w-1
ss = [SI() for _ in range(h)]

if ss[0][0] != ss[h-1][w-1]:
    print(0)
    exit()

dij = [(0, -1, 0, 1), (-1, 0, 0, 1), (0, -1, 1, 0), (-1, 0, 1, 0)]

dp = defaultdict(int)
mid = (n-1)//2
if n & 1:
    for i in range(h):
        j = mid-i
        if not 0 <= j < w: continue
        dp[i, j, i, j] = 1
else:
    for i in range(h):
        j = mid-i
        if not 0 <= j < w: continue
        for u, v in [(i+1, j), (i, j+1)]:
            if 0 <= u < h and 0 <= v < w and ss[i][j] == ss[u][v]:
                dp[i, j, u, v] = 1

for _ in range(mid):
    ndp = defaultdict(int)
    for (i, j, u, v), pre in dp.items():
        for di, dj, du, dv in dij:
            ni, nj, nu, nv = i+di, j+dj, u+du, v+dv
            if not 0 <= ni < h: continue
            if not 0 <= nj < w: continue
            if not 0 <= nu < h: continue
            if not 0 <= nv < w: continue
            if ss[ni][nj] != ss[nu][nv]: continue
            ndp[ni, nj, nu, nv] += pre
            ndp[ni, nj, nu, nv] %= md
    dp = ndp
# pDB(dp)

print(dp[0, 0, h-1, w-1])
0