結果

問題 No.1916 Making Palindrome on Gird
ユーザー wolgnikwolgnik
提出日時 2022-04-29 22:04:05
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,348 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 847,084 KB
最終ジャッジ日時 2023-09-11 13:16:22
合計ジャッジ時間 4,273 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,884 KB
testcase_01 AC 71 ms
71,416 KB
testcase_02 AC 143 ms
78,492 KB
testcase_03 AC 95 ms
71,604 KB
testcase_04 AC 102 ms
73,072 KB
testcase_05 AC 102 ms
73,052 KB
testcase_06 AC 102 ms
72,880 KB
testcase_07 AC 102 ms
73,048 KB
testcase_08 AC 103 ms
73,216 KB
testcase_09 AC 101 ms
73,128 KB
testcase_10 AC 94 ms
71,628 KB
testcase_11 AC 110 ms
77,948 KB
testcase_12 AC 102 ms
72,624 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 * W) for _ in range(H * W)]
dp[0][-1] = 1
vis = [[0] * (H * W) for _ in range(H * W)]

while len(Q):
  t = Q.popleft()
  u = t // base
  v = t % base
  if vis[u][v]: continue
  vis[u][v] = 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][v2] += dp[u][v]
      dp[u2][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][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][u * W + v]
        res %= mod
print(res)
0