結果

問題 No.1916 Making Palindrome on Gird
ユーザー wolgnikwolgnik
提出日時 2022-04-29 22:07:27
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,347 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 86,940 KB
実行使用メモリ 842,740 KB
最終ジャッジ日時 2023-09-11 13:21:04
合計ジャッジ時間 3,855 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,788 KB
testcase_01 AC 74 ms
71,224 KB
testcase_02 AC 143 ms
80,040 KB
testcase_03 AC 95 ms
71,544 KB
testcase_04 AC 101 ms
73,100 KB
testcase_05 AC 100 ms
73,208 KB
testcase_06 AC 101 ms
72,752 KB
testcase_07 AC 102 ms
73,160 KB
testcase_08 AC 102 ms
73,996 KB
testcase_09 AC 100 ms
72,844 KB
testcase_10 AC 94 ms
71,484 KB
testcase_11 AC 109 ms
77,768 KB
testcase_12 AC 101 ms
72,324 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
from collections import deque as dq
Q = dq([H * W - 1])

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

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

while len(Q):
  t = Q.popleft()
  u = t // base
  v = t % base
  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
      Q.append(u2 * base + v2)
      #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