鱼C论坛

 找回密码
 立即注册
查看: 1565|回复: 2

[已解决]遍历问题

[复制链接]
发表于 2019-11-27 16:16:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
西蒙.丹尼斯.泊松是著名的法国数学家和物理学家。据说在他遇到某个古老的谜题之后,就开始对数学感兴趣了,这个谜题是这样的:给定一个装满水的8品脱壶以及两个容量分别为5品脱和3品脱的空壶,如何通过完全灌满或者到空这些壶从而使得某个壶精确地装有4品脱的水?用广度优先查找来求解这个谜题。要求在输出结果中包含广度优先的遍历过程(结点的遍历顺序)。
最好可以设计并实现可视化的演示过程。
最佳答案
2019-11-27 20:04:57
  1. import queue

  2. def possible(list3):
  3.     list1 = list3[-1]
  4.     result = []
  5.     list2 = [8,5,3]
  6.     temp1 = [True if list1[i] > 0 else False for i in range(3)]
  7.     temp2 = [True if list1[i] != list2[i] else False for i in range(3)]
  8.     for i in range(3):
  9.         if temp1[i] == True:
  10.             for j in range(3):
  11.                 if temp2[j] == True:
  12.                     if i != j:
  13.                         temp = list1.copy()
  14.                         if list2[j] - list1[j] >= list1[i]:
  15.                             temp[i] = 0
  16.                             temp[j] += list1[i]
  17.                             list4 = list3.copy()
  18.                             list4.append(temp)
  19.                             result.append(list4)
  20.                         else:
  21.                             temp[i] -= list2[j] - list1[j]
  22.                             temp[j] = list2[j]
  23.                             list4 = list3.copy()
  24.                             list4.append(temp)
  25.                             result.append(list4)
  26.     return result
  27.                     

  28. list2 = [8,0,0]
  29. array = queue.Queue()
  30. array.put([list2])
  31. flag = False
  32. while not (array.empty() or flag == True):
  33.     temp = array.get()
  34.     temp2 = possible(temp)
  35.     for each in temp2:
  36.         if not 4 in each[-1]:
  37.             array.put(each)
  38.         else:
  39.             print('解答')
  40.             print(each)
  41.             flag = True
  42.             break
复制代码

不知道这个算不算广度优先。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-27 20:04:57 | 显示全部楼层    本楼为最佳答案   
  1. import queue

  2. def possible(list3):
  3.     list1 = list3[-1]
  4.     result = []
  5.     list2 = [8,5,3]
  6.     temp1 = [True if list1[i] > 0 else False for i in range(3)]
  7.     temp2 = [True if list1[i] != list2[i] else False for i in range(3)]
  8.     for i in range(3):
  9.         if temp1[i] == True:
  10.             for j in range(3):
  11.                 if temp2[j] == True:
  12.                     if i != j:
  13.                         temp = list1.copy()
  14.                         if list2[j] - list1[j] >= list1[i]:
  15.                             temp[i] = 0
  16.                             temp[j] += list1[i]
  17.                             list4 = list3.copy()
  18.                             list4.append(temp)
  19.                             result.append(list4)
  20.                         else:
  21.                             temp[i] -= list2[j] - list1[j]
  22.                             temp[j] = list2[j]
  23.                             list4 = list3.copy()
  24.                             list4.append(temp)
  25.                             result.append(list4)
  26.     return result
  27.                     

  28. list2 = [8,0,0]
  29. array = queue.Queue()
  30. array.put([list2])
  31. flag = False
  32. while not (array.empty() or flag == True):
  33.     temp = array.get()
  34.     temp2 = possible(temp)
  35.     for each in temp2:
  36.         if not 4 in each[-1]:
  37.             array.put(each)
  38.         else:
  39.             print('解答')
  40.             print(each)
  41.             flag = True
  42.             break
复制代码

不知道这个算不算广度优先。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-11-28 12:24:54 | 显示全部楼层
danteer 发表于 2019-11-27 20:04
不知道这个算不算广度优先。

谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 16:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表