結果
| 問題 |
No.1916 Making Palindrome on Gird
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-16 04:09:20 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,455 bytes |
| コンパイル時間 | 1,015 ms |
| コンパイル使用メモリ | 82,844 KB |
| 実行使用メモリ | 331,720 KB |
| 最終ジャッジ日時 | 2025-03-16 04:09:34 |
| 合計ジャッジ時間 | 10,609 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 WA * 2 TLE * 2 -- * 14 |
ソースコード
## https://yukicoder.me/problems/no/1916
from collections import deque
MOD = 10 ** 9 + 7
def main():
H, W = map(int, input().split())
S = []
for _ in range(H):
S.append(input())
if S[0][0] != S[H - 1][W - 1]:
print(0)
return
dp = {(0, 0, H - 1, W - 1): 1}
queue = deque()
queue.append((0, 0, H - 1, W - 1))
answer = 0
while len(queue) > 0:
h0, w0, h1, w1 = queue.popleft()
x = dp[(h0, w0, h1, w1)]
if abs(h0 - h1) + abs(w0 - w1) <= 1:
answer += x
answer %= MOD
continue
for dh0, dw0 in ((1, 0), (0, 1)):
new_h0 = dh0 + h0
new_w0 = dw0 + w0
if not (0 <= new_h0 < H and 0 <= new_w0 < W):
continue
for dh1, dw1 in ((-1, 0), (0, - 1)):
new_h1 = dh1 + h1
new_w1 = dw1 + w1
if not (0 <= new_h1 < H and 0 <= new_w1 < W):
continue
if S[new_h0][new_w0] == S[new_h1][new_w1]:
y = (new_h0, new_w0, new_h1, new_w1)
queued = True
if y not in dp:
dp[y] = 0
queued = False
dp[y] += x
dp[y] %= MOD
if not queued:
queue.append(y)
print(answer)
if __name__ == "__main__":
main()