Question3
来源:互联网 发布:淘宝变色玻璃杯 编辑:程序博客网 时间:2024/06/07 23:08
#每一个节点可以走的位置zhaofa={0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[3,9,0],5:[],6:[1,7,0],7:[2,6],8:[1,3],9:[2,4]}level=0times=0N=4#当前的位置ongoing_nodes=[1]#判断是否结束 1 到达了N步 2 前边没有可以走的while(level<=N or len(ongoing_nodes)==0): print (str(level)) print(ongoing_nodes) #判断当前要走的节点集合中有没有包含目标节点 如果有就去除并且计数器加一 while True: if 9 in ongoing_nodes: times+=1 ongoing_nodes.remove(9) else: break #广度优先搜索 开始下一步走 next_step_nodes=[] for pos in ongoing_nodes: next_step_nodes.extend(zhaofa[pos]) # 走到下一步 ongoing_nodes=next_step_nodes #层数加一 level+=1print(times)
输出结果是
0[1]1[6, 8]2[1, 7, 0, 1, 3]3[6, 8, 2, 6, 4, 6, 6, 8, 4, 8]4[1, 7, 0, 1, 3, 7, 9, 1, 7, 0, 3, 9, 0, 1, 7, 0, 1, 7, 0, 1, 3, 3, 9, 0, 1, 3]3
阅读全文
0 0
- Question3
- question3
- question3.A letter to the Editor
- leetcode question3.Longest Substring Without Repeating Characters
- mybatis在oracle中的一点小结
- 阿里云服务器3306端口MySQL连接异常
- Wireshark协议源代码
- java上转型和下转型对象
- 洛谷P1471 方差
- Question3
- 【转】深入理解类加载机制
- Vue.js 2.5新特性介绍
- 《I, Robot》機器人是人類的公敵?
- python安全相关库,简单介绍
- JPA高级映射
- 实现Register&Login过程中遇到的问题
- css基础-选择器
- scala 隐式转换(一)