結果

問題 No.1916 Making Palindrome on Gird
ユーザー ああいいああいい
提出日時 2022-04-29 21:52:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,891 bytes
コンパイル時間 2,134 ms
コンパイル使用メモリ 86,708 KB
実行使用メモリ 266,416 KB
最終ジャッジ日時 2023-09-11 12:55:12
合計ジャッジ時間 27,732 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,592 KB
testcase_01 AC 74 ms
71,112 KB
testcase_02 AC 137 ms
78,412 KB
testcase_03 AC 95 ms
71,700 KB
testcase_04 AC 100 ms
76,212 KB
testcase_05 AC 102 ms
76,264 KB
testcase_06 AC 102 ms
76,284 KB
testcase_07 AC 102 ms
76,204 KB
testcase_08 AC 100 ms
76,204 KB
testcase_09 AC 102 ms
76,080 KB
testcase_10 AC 94 ms
71,620 KB
testcase_11 AC 102 ms
76,244 KB
testcase_12 AC 102 ms
76,048 KB
testcase_13 AC 136 ms
78,292 KB
testcase_14 AC 136 ms
78,448 KB
testcase_15 AC 547 ms
99,944 KB
testcase_16 AC 446 ms
90,960 KB
testcase_17 AC 946 ms
134,228 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 78 ms
71,452 KB
testcase_24 AC 77 ms
71,320 KB
testcase_25 AC 78 ms
71,456 KB
testcase_26 AC 77 ms
71,316 KB
testcase_27 AC 77 ms
71,320 KB
testcase_28 TLE -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

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