AtCoderのこの問題で
https://atcoder.jp/contests/abc119/tasks/abc119_c
python3
1n,goalA,goalB,goalC = list(map(int, input().split())) 2l = [int(input()) for _ in range(n)] 3cost = [] 4depth = 0 5 6def dfs(depth: int, a: int, b: int, c:int): 7 if depth == n: 8 if not a == 0 and b == 0 and c == 0: 9 cost.append(min(a - goalA) + min(b - goalB) + min(c - goalC) - 30) 10 else: 11 cost.append(10**8) 12 13 dfs(depth+1, a, b, c) + 10 14 dfs(depth+1, a + l[depth], b, c) + 10 15 dfs(depth+1, a , b + l[depth], c) + 10 16 dfs(depth+1, a , b, c + l[depth]) + 10 17 18dfs(0,0,0,0) 19print(min(cost))
と書いたのですが、実行して見ると標準エラー出力に
Traceback (most recent call last): File "./Main.py", line 19, in <module> dfs(0,0,0,0) File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c) + 10 File "./Main.py", line 14, in dfs dfs(depth+1, a, b, c...
と無限ループしてしまいうまく動かず困っています。
depthなどの渡し方がまずいのだろうかと思いつつ、Pythonの値の参照渡しなどの挙動を調べてもうまく動かないのでどなたか詳しい方解決法をお教えいただけませんか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/16 01:11