結果

問題 No.1916 Making Palindrome on Gird
ユーザー ああいい
提出日時 2022-04-29 21:52:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,891 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 265,256 KB
最終ジャッジ日時 2024-06-29 03:17:24
合計ジャッジ時間 23,574 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 TLE * 4 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W = map(int,input().split())
S = [input() for _ in range(H)]
P = 10 ** 9 + 7

import sys
if S[0][0] != S[-1][-1]:
    print(0)
    exit()
from collections import defaultdict
d = defaultdict(int)
d[(0,0,H - 1,W - 1)] = 1
ans = 0
u = H + W - 2
if u % 2 == 0:
    for _ in range(u // 2):
        nx = defaultdict(int)
        for h,w,hh,ww in d:
            for i,j in [(1,0),(0,1)]:
                for k,l in [(-1,0),(0,-1)]:
                    if h + i < H and w + j < W and hh + k >= 0 and ww + l >= 0:
                        if S[h + i][w + j] == S[hh + k][ww + l]:
                            nx[(h + i,w + j,hh + k,ww + l)] += d[(h,w,hh,ww)]
                            nx[(h + i,w + j,hh + k,ww + l)] %= P
        d = nx
    for h,w,hh,ww in d:
        if h == hh and w == ww:
            ans += d[(h,w,hh,ww)]
            ans %= P
    print(ans)
else:
    for _ in range(u // 2 + 1):
        nx = defaultdict(int)
        if _ < u // 2:
            for h,w,hh,ww in d:
                for i,j in [(1,0),(0,1)]:
                    for k,l in [(-1,0),(0,-1)]:
                        if h + i < H and w + j < W and hh + k >= 0 and ww + l >= 0:
                            if S[h + i][w + j] == S[hh + k][ww + l]:
                                nx[(h + i,w + j,hh + k,ww + l)] += d[(h,w,hh,ww)]
                                nx[(h + i,w + j,hh + k,ww + l)] %= P
        else:
            for h,w,hh,ww in d:
                for i,j in [(1,0),(0,1)]:
                    for k,l in [(-1,0),(0,-1)]:
                        if h + i < H and w + j < W and hh + k >= 0 and ww + l >= 0:
                            if S[h + i][w + j] == S[hh + k][ww + l]:
                                if h + i == hh and w + j == ww and hh + k == h and ww + l == w:
                                    ans += d[(h,w,hh,ww)]
                                    ans %= P
        d = nx
    print(ans)
0