## 问题:
公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
用100文钱买一百只鸡,如何购买
## 状态转移解法(递归)
# (公鸡数,母鸡数,小鸡数,总钱数,总鸡数),状态转移
import numpy as np # 导入numpy库
st = np.zeros((100, 100, 100)) # 新建三维数组,且初始值为0,记录状态是否被遍历过,进行剪枝操作
def resolve(a, b, c, d, e):
if d == 100 and e == 100:
print("%s只公鸡,%s只母鸡,%s只小鸡" % (a, b, c))
else:
a1 = a + 1
b1 = b + 1
c1 = c + 3
if d + 5 <= 100 and e + 1 <= 100 and st[a1][b][c] == 0:
st[a1][b][c] = 1
resolve(a1, b, c, d + 5, e + 1)
if d + 3 <= 100 and e + 1 <= 100 and st[a][b1][c] == 0:
st[a][b1][c] = 1
resolve(a, b1, c, d + 3, e + 1)
if d + 1 <= 100 and e + 3 <= 100 and st[a][b][c1] == 0:
st[a][b][c1] = 1
resolve(a, b, c1, d + 1, e + 3)
# (公鸡数,母鸡数,小鸡数,总钱数,总鸡数)
resolve(0, 0, 0, 0, 0)
## 运行结果: