結果

問題 No.1916 Making Palindrome on Gird
ユーザー lilictakalilictaka
提出日時 2022-04-29 23:10:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,823 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 85,964 KB
最終ジャッジ日時 2023-09-11 14:41:15
合計ジャッジ時間 6,259 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,636 KB
testcase_01 AC 94 ms
71,608 KB
testcase_02 AC 104 ms
77,160 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 97 ms
72,192 KB
testcase_10 AC 92 ms
71,304 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 153 ms
80,456 KB
testcase_24 AC 147 ms
80,748 KB
testcase_25 AC 150 ms
81,020 KB
testcase_26 AC 150 ms
80,572 KB
testcase_27 AC 144 ms
81,004 KB
testcase_28 AC 177 ms
83,812 KB
testcase_29 WA -
testcase_30 AC 177 ms
83,724 KB
testcase_31 AC 177 ms
83,560 KB
testcase_32 AC 181 ms
83,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

H,W = map(int,input().split())
S = []
edge = [[[] for _ in range(W)] for _ in range(H)]
IN = [[0] * W for _ in range(H)]
flag = [[False] * W for _ in range(H)]
def check(h,w):
    if 0 <= h < H and 0 <= w < W:
        return True
    else:
        return False
def e(h,w,t):
    for dh,dw in [(-1,0),(1,0),(0,1),(0,-1)]:
        nh = h + dh 
        nw = w + dw
        if check(nh,nw) :
            if flag[nh][nw]:
                if t == 0:
                    edge[h][w].append((nh,nw))
                    IN[nh][nw] += 1
                else:
                    edge[nh][nw].append((h,w))
                    IN[h][w] += 1

for _ in range(H):
    s = list(map(lambda x:ord(x) - ord('a') ,list(input())))
    S.append(s)

def solve():
    for i in range((H+W-2)//2 + 1):
        k = (H + W -2)//2 - i
        left = set()
        right = set()
        for h in range(max(0,k-(W-1)),min(H-1,k) + 1):
            w = k - h
            left.add(S[h][w])
        rk = (H+W-2 + 1)//2 + i
        for h in range(max(0,rk-(W-1)),min(H-1,rk) + 1):
            w = rk - h
            right.add(S[h][w])
        for h in range(max(0,k-(W-1)),min(H-1,k) + 1):
            w = k - h
            if S[h][w] in right:
                flag[h][w] = True
                e(h,w,0)
        for h in range(max(0,rk-(W-1)),min(H-1,rk) + 1):
            w = rk - h
            if S[h][w] in left:
                flag[h][w] = True
                e(h,w,1)
solve()
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1
mod = 10 ** 9 + 7
for h in range(H):
    for w in range(W):
        if check(h-1,w):
            if flag[h-1][w]:
                dp[h][w] += dp[h-1][w]
        if check(h,w-1):
            if flag[h][w-1]:
                dp[h][w] += dp[h][w-1]
        dp[h][w] %= mod
print(dp[H-1][W-1])

       
0