結果
| 問題 | 
                            No.1916 Making Palindrome on Gird
                             | 
                    
| コンテスト | |
| ユーザー | 
                             titia
                         | 
                    
| 提出日時 | 2022-04-29 22:11:07 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,352 bytes | 
| コンパイル時間 | 151 ms | 
| コンパイル使用メモリ | 82,300 KB | 
| 実行使用メモリ | 271,084 KB | 
| 最終ジャッジ日時 | 2024-06-29 03:39:55 | 
| 合計ジャッジ時間 | 26,214 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 20 TLE * 6 -- * 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