結果

問題 No.611 Day of the Mountain
ユーザー vwxyzvwxyz
提出日時 2022-04-28 02:32:25
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,232 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 259,620 KB
最終ジャッジ日時 2023-09-10 16:44:45
合計ジャッジ時間 6,618 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,441 ms
259,620 KB
testcase_01 RE -
testcase_02 AC 1,516 ms
259,556 KB
testcase_03 RE -
testcase_04 AC 99 ms
77,304 KB
testcase_05 AC 96 ms
76,320 KB
testcase_06 AC 77 ms
71,444 KB
testcase_07 AC 75 ms
71,052 KB
testcase_08 RE -
testcase_09 AC 72 ms
71,388 KB
testcase_10 AC 72 ms
71,280 KB
testcase_11 AC 80 ms
76,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

H,W=map(int,readline().split())
A=[list(readline().rstrip()) for h in range(H)]
if H<W:
    A=[[A[h][w] for h in range(H)] for w in range(W)]
    H,W=W,H
inf=1<<30
dist=[[inf]*W for h in range(H)]
dist[0][0]=int(A[0][0])
for h in range(H):
    for w in range(W):
        if A[h][w]=="?":
            d=1
        else:
            d=int(A[h][w])
        if h:
            dist[h][w]=min(dist[h][w],dist[h-1][w]+d)
        if w:
            dist[h][w]=min(dist[h][w],dist[h][w-1]+d)
B=[[0]*W for h in range(H)]
B[H-1][W-1]=1
for h in range(H-1,-1,-1):
    for w in range(W-1,-1,-1):
        if B[h][w]:
            if A[h][w]=="?":
                d=1
            else:
                d=int(A[h][w])
            if h and dist[h-1][w]+d==dist[h][w]:
                B[h-1][w]=1
            if w and dist[h][w-1]+d==dist[h][w]:
                B[h][w-1]=1
for h in range(H):
    for w in range(W):
        if B[h][w]:
            if A[h][w]=="?":
                B[h][w]=2
            else:
                A[h][w]=int(A[h][w])
mod=201712111
dp=[0]*(1<<W)
for w in range(1,W+1):
    if all(B[0][ww] for ww in range(w)):
        if w==W or B[0][w]==0:
            dp[(1<<w)-1]=pow(9,A[0][w:].count("?"),mod)
        elif B[0][w]==2:
            dp[(1<<w)-1]=pow(9,A[0][w+1:].count("?"),mod)*8%mod
for hw in range(W+1,H*W+1):
    h,w=divmod(hw-1,W)
    prev=dp
    dp=[0]*(1<<W)
    for bit in range(1<<W):
        if B[h][w]==1:
            if w and bit&1<<W-1 and dist[h][w-1]+A[h][w]==dist[h][w] or bit&1 and dist[h-1][w]+A[h][w]==dist[h][w]:
                dp[bit>>1|1<<W-1]+=prev[bit]
            else:
                dp[bit>>1]+=prev[bit]
        elif B[h][w]==2:
            if w and bit&1<<W-1 and dist[h][w-1]+1==dist[h][w] or bit&1 and dist[h-1][w]+1==dist[h][w]:
                dp[bit>>1|1<<W-1]+=prev[bit]
                dp[bit>>1]+=8*prev[bit]
            else:
                dp[bit>>1]+=9*prev[bit]
        else:
            if A[h][w]=="?":
                dp[bit>>1]+=prev[bit]*9
            else:
                dp[bit>>1]+=prev[bit]
    for bit in range(1<<W):
        dp[bit]%=mod
ans=sum(dp[bit] for bit in range(1<<W) if bit&1<<W-1)%mod
print(dist[H-1][W-1])
print(ans)
0