結果
| 問題 |
No.1916 Making Palindrome on Gird
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2022-04-29 22:08:37 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,353 bytes |
| コンパイル時間 | 241 ms |
| コンパイル使用メモリ | 82,296 KB |
| 実行使用メモリ | 270,824 KB |
| 最終ジャッジ日時 | 2024-06-29 03:37:21 |
| 合計ジャッジ時間 | 23,949 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 WA * 3 TLE * 5 -- * 4 |
ソースコード
import sys
input = sys.stdin.readline
from collections import defaultdict
mod=10**9+7
H,W=map(int,input().split())
MAP=[input().strip() for i in range(H)]
if MAP[0][0]!=MAP[-1][-1]:
print(0)
exit()
DP=dict()
DP[0,0,H-1,W-1]=1
for i in range((H+W-2)//2):
NDP=defaultdict(int)
for x,y,z,w in DP:
if 0<=x<H and 0<=z<H and 0<=y+1<W and 0<=w-1<W and MAP[x][y+1]==MAP[z][w-1]:
NDP[x,y+1,z,w-1]+=DP[x,y,z,w]
NDP[x,y+1,z,w-1]%=mod
if 0<=x<H and 0<=z-1<H and 0<=y+1<W and 0<=w<W and MAP[x][y+1]==MAP[z-1][w]:
NDP[x,y+1,z-1,w]+=DP[x,y,z,w]
NDP[x,y+1,z-1,w]%=mod
if 0<=x+1<H and 0<=z<H and 0<=y<W and 0<=w-1<W and MAP[x+1][y]==MAP[z][w-1]:
NDP[x+1,y,z,w-1]+=DP[x,y,z,w]
NDP[x+1,y,z,w-1]%=mod
if 0<=x+1<H and 0<=z-1<H and 0<=y<W and 0<=w<W and MAP[x+1][y]==MAP[z-1][w]:
NDP[x+1,y,z-1,w]+=DP[x,y,z,w]
NDP[x+1,y,z-1,w]%=mod
DP=NDP
if (H+W)%2==0:
ANS=0
for x,y,z,w in DP:
if x==z and y==w:
ANS+=DP[x,y,z,w]
ANS%=mod
print(ANS)
else:
ANS=0
for x,y,z,w in DP:
if x==z and y+1==w:
ANS+=DP[x,y,z,w]
ANS%=mod
if x+1==-z and y==w:
ANS+=DP[x,y,z,w]
ANS%=mod
print(ANS)
titia