题解 | dfs递归#24点游戏算法#
24点游戏算法
http://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
import copy
def dps(num):
if len(num)==1:
return abs(abs(num[0]) - 24) < 0.00001
for i in range(len(num)):
for j in range(i+1, len(num)):
n1,n2= num[i], num[j]
rst = num[:i] + num[i+1:j] + num[j+1:]
if dps([n1+n2]+rst):return True
if dps([n1-n2]+rst):return True
if dps([n1*n2]+rst):return True
if n1!=0:
if dps([n2/n1]+rst):
return True
if n2!=0:
if dps([n1/n2]+rst):
return True
return False
while True:
try:
x = list(map(int,input().split()))
print("true" if dps(x) else "false")
except:
break