結果

問題 No.1916 Making Palindrome on Gird
ユーザー wolgnikwolgnik
提出日時 2022-04-29 22:14:13
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,477 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 841,880 KB
最終ジャッジ日時 2023-09-11 13:30:19
合計ジャッジ時間 3,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,280 KB
testcase_01 AC 65 ms
71,288 KB
testcase_02 AC 112 ms
79,716 KB
testcase_03 AC 67 ms
71,360 KB
testcase_04 AC 91 ms
77,660 KB
testcase_05 AC 95 ms
78,016 KB
testcase_06 AC 91 ms
77,364 KB
testcase_07 AC 94 ms
77,588 KB
testcase_08 AC 95 ms
78,408 KB
testcase_09 AC 69 ms
71,908 KB
testcase_10 AC 63 ms
71,296 KB
testcase_11 AC 90 ms
77,208 KB
testcase_12 AC 86 ms
76,932 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
H, W = map(int, input().split())
a = [list(input())[: -1] for _ in range(H)]
mod = 10 ** 9 + 7

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

base = H * W

d1 = [1, 0]
d2 = [-1, 0]

dp = [0] * (H * H * W * W)
dp[H * W - 1] = 1
vis = [0] * (H * H * W * W)

s = [[] for _ in range(H + W - 1)]
for i in range(H):
  for j in range(W): s[i + j].append((i * W + j))

for turn in range(H + W - 2):
  for u in s[turn]:
    for v in s[H + W - 2 - turn]:
      t = u * base + v
      if vis[t]: continue
      vis[t] = 1

      i, j = u // W, u % W
      y, x = v // W, v % W

      for k in range(2):
        i2, j2 = i + d1[k], j + d1[~k]
        if not (i2 in range(H) and j2 in range(W)): continue
        u2 = i2 * W + j2
        for kk in range(2):
          y2, x2 = y + d2[kk], x + d2[~kk]
          if not (y2 in range(H) and x2 in range(W)): continue
          if a[i2][j2] != a[y2][x2]: continue
          v2 = y2 * W + x2

          dp[u2 * base + v2] += dp[t]
          dp[u2 * base + v2] %= mod
          #print((i, j), (y,x),(i2, j2),(y2,x2))

      #print((i, j), (y, x), len(Q))

res = 0
for i in range(H):
  for j in range(W):
    res += dp[(i * W + j) * base + i * W + j]
    res %= mod
for i in range(H):
  for j in range(W):
    for k in range(2):
      u, v = i + d1[k], j + d1[~k]
      if u in range(H) and v in range(W) and a[i][j] == a[u][v]:
        res += dp[(i * W + j) * base + u * W + v]
        res %= mod
print(res)
0