結果
| 問題 | No.2708 Jewel holder | 
| コンテスト | |
| ユーザー |  FromBooska | 
| 提出日時 | 2024-03-31 16:52:52 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 848 ms / 2,000 ms | 
| コード長 | 1,489 bytes | 
| コンパイル時間 | 154 ms | 
| コンパイル使用メモリ | 82,436 KB | 
| 実行使用メモリ | 83,696 KB | 
| 最終ジャッジ日時 | 2024-09-30 21:07:30 | 
| 合計ジャッジ時間 | 2,992 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
# 盤面が非常に小さい、visited管理してDFSにしよう
# 問題文では「没収されるときに1つも宝石を持っていない状況」とあるが0個になるのはOKなのか
# 動きが上下左右だけというのも書いてない
H, W = map(int, input().split())
A = []
for i in range(H):
    A.append(input())
    
import sys
sys.setrecursionlimit(10**7)
def dfs(ch, cw, step, coin):
    global ans_list, min_step    
    
    #print('ch', ch, 'cw', cw, 'step', step, 'coin', coin)
    #print('visited', visited)
    #print()
    if step > min_step:
        return
    if (ch, cw)==(H-1, W-1):
        ans_list.append(step)
        if step < min_step:
            min_step = step
        return
    
    for dh, dw in d:
        if 0 <= ch+dh < H and 0 <= cw+dw < W and visited[ch+dh][cw+dw ]==0:
            if A[ch+dh][cw+dw] == 'o':
                visited[ch+dh][cw+dw ]=1
                dfs(ch+dh, cw+dw, step+1, coin+1)
                visited[ch+dh][cw+dw ]=0
            elif A[ch+dh][cw+dw] == 'x':
                if coin>=1:
                    visited[ch+dh][cw+dw ]=1
                    dfs(ch+dh, cw+dw, step+1, coin-1)
                    visited[ch+dh][cw+dw ]=0
    
d = [[+1, 0], [-1, 0], [0, +1], [0, -1]]
min_step = 10**3
ans_list = []
visited = [[0]*W for i in range(H)]
visited[0][0] = 1
dfs(0, 0, 0, 1)
#print(ans_list)
if len(ans_list)>0:
    min_step = min(ans_list)
    ans = ans_list.count(min_step)
else:
    ans = 0
print(ans)
            
            
            
        