結果

問題 No.611 Day of the Mountain
ユーザー vwxyzvwxyz
提出日時 2022-04-28 02:42:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,401 ms / 2,017 ms
コード長 2,422 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 259,988 KB
最終ジャッジ日時 2023-09-10 16:45:44
合計ジャッジ時間 7,959 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,035 ms
259,708 KB
testcase_01 AC 970 ms
259,748 KB
testcase_02 AC 1,286 ms
259,556 KB
testcase_03 AC 1,401 ms
259,988 KB
testcase_04 AC 105 ms
77,208 KB
testcase_05 AC 98 ms
76,452 KB
testcase_06 AC 81 ms
71,328 KB
testcase_07 AC 79 ms
71,388 KB
testcase_08 AC 113 ms
77,268 KB
testcase_09 AC 76 ms
71,268 KB
testcase_10 AC 76 ms
71,472 KB
testcase_11 AC 88 ms
76,188 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)]
if A[0][0]=="?":
    dist[0][0]=1
else:
    dist[0][0]=int(A[0][0])
for h in range(H):
    for w in range(W):
        if A[h][w]=="?":
            d=1
        else:
            A[h][w]=int(A[h][w])
            d=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
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]==1 and A[0][w]=="?":
            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]:
            if A[h][w]=="?":
                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]
                    dp[bit>>1|1<<W-1]%=mod
                    dp[bit>>1]%=mod
                else:
                    dp[bit>>1]+=9*prev[bit]
                    dp[bit>>1]%=mod
            else:
                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]
                    dp[bit>>1|1<<W-1]%=mod
                else:
                    dp[bit>>1]+=prev[bit]
                    dp[bit>>1]%=mod
        else:
            if A[h][w]=="?":
                dp[bit>>1]+=prev[bit]*9
                dp[bit>>1]%=mod
            else:
                dp[bit>>1]+=prev[bit]
                dp[bit>>1]%=mod
ans=0
for bit in range(1<<W-1,1<<W):
    ans+=dp[bit]
    ans%=mod
print(dist[H-1][W-1])
print(ans)
0