結果

問題 No.1916 Making Palindrome on Gird
ユーザー titiatitia
提出日時 2022-04-29 22:41:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,111 ms / 3,000 ms
コード長 2,112 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 154,188 KB
最終ジャッジ日時 2023-09-11 14:04:31
合計ジャッジ時間 13,806 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,248 KB
testcase_01 AC 73 ms
71,252 KB
testcase_02 AC 107 ms
78,064 KB
testcase_03 AC 73 ms
71,512 KB
testcase_04 AC 74 ms
71,396 KB
testcase_05 AC 73 ms
71,252 KB
testcase_06 AC 76 ms
71,392 KB
testcase_07 AC 73 ms
71,180 KB
testcase_08 AC 74 ms
71,388 KB
testcase_09 AC 73 ms
71,248 KB
testcase_10 AC 73 ms
71,568 KB
testcase_11 AC 74 ms
71,176 KB
testcase_12 AC 75 ms
71,368 KB
testcase_13 AC 116 ms
77,300 KB
testcase_14 AC 94 ms
76,292 KB
testcase_15 AC 382 ms
81,784 KB
testcase_16 AC 374 ms
81,940 KB
testcase_17 AC 445 ms
82,100 KB
testcase_18 AC 731 ms
103,868 KB
testcase_19 AC 731 ms
99,132 KB
testcase_20 AC 725 ms
98,388 KB
testcase_21 AC 733 ms
102,020 KB
testcase_22 AC 728 ms
99,644 KB
testcase_23 AC 75 ms
71,284 KB
testcase_24 AC 74 ms
71,456 KB
testcase_25 AC 75 ms
71,368 KB
testcase_26 AC 75 ms
71,420 KB
testcase_27 AC 73 ms
71,524 KB
testcase_28 AC 1,084 ms
150,280 KB
testcase_29 AC 1,111 ms
154,188 KB
testcase_30 AC 1,051 ms
148,664 KB
testcase_31 AC 1,049 ms
148,524 KB
testcase_32 AC 1,073 ms
148,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=10**9+7

H,W=map(int,input().split())
MAP=[input().strip() for i in range(H)]

#H,W=200,200
#MAP=["a"*W for i in range(H)]

if MAP[0][0]!=MAP[-1][-1]:
    print(0)
    exit()
def cc(x,y,z,w):
    return x*8000000+y*40000+z*200+w

def re(r):
    w=r%200
    r//=200
    z=r%200
    r//=200
    y=r%200
    x=r//200

    return x,y,z,w

DP=dict()
DP[cc(0,0,H-1,W-1)]=1

for i in range((H+W-2)//2):
    NDP=dict()

    for r in DP:
        x,y,z,w=re(r)
        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]:
            if cc(x,y+1,z,w-1) in NDP:
                NDP[cc(x,y+1,z,w-1)]+=DP[cc(x,y,z,w)]
                NDP[cc(x,y+1,z,w-1)]%=mod
            else:
                NDP[cc(x,y+1,z,w-1)]=DP[cc(x,y,z,w)]
                

        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]:
            if cc(x,y+1,z-1,w) in NDP:
                NDP[cc(x,y+1,z-1,w)]+=DP[cc(x,y,z,w)]
                NDP[cc(x,y+1,z-1,w)]%=mod
            else:
                NDP[cc(x,y+1,z-1,w)]=DP[cc(x,y,z,w)]

        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]:
            if cc(x+1,y,z,w-1) in NDP:
                NDP[cc(x+1,y,z,w-1)]+=DP[cc(x,y,z,w)]
                NDP[cc(x+1,y,z,w-1)]%=mod
            else:
                NDP[cc(x+1,y,z,w-1)]=DP[cc(x,y,z,w)]

        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]:
            if cc(x+1,y,z-1,w) in NDP:
                NDP[cc(x+1,y,z-1,w)]+=DP[cc(x,y,z,w)]
                NDP[cc(x+1,y,z-1,w)]%=mod
            else:
                NDP[cc(x+1,y,z-1,w)]=DP[cc(x,y,z,w)]

    DP=NDP

if (H+W)%2==0:
    ANS=0
    for r in DP:
        x,y,z,w=re(r)
        if x==z and y==w:
            ANS+=DP[cc(x,y,z,w)]
            ANS%=mod
    print(ANS)
    
else:
    ANS=0
    for r in DP:
        x,y,z,w=re(r)
        if x==z and y+1==w:
            ANS+=DP[cc(x,y,z,w)]
            ANS%=mod
        if x+1==z and y==w:
            ANS+=DP[cc(x,y,z,w)]
            ANS%=mod
            
    print(ANS)
    
    
0