网易2017内推笔试2:解救小易 [python]
来源:互联网 发布:网络销售语言技巧 编辑:程序博客网 时间:2024/06/05 07:17
'''[编程题] 解救小易时间限制:1秒空间限制:32768K有一片1000*1000的草地,小易初始站在(1,1)(最左上角的位置)。小易在每一秒会横向或者纵向移动到相邻的草地上吃草(小易不会走出边界)。大反派超超想去捕捉可爱的小易,他手里有n个陷阱。第i个陷阱被安置在横坐标为xi ,纵坐标为yi 的位置上,小易一旦走入一个陷阱,将会被超超捕捉。你为了去解救小易,需要知道小易最少多少秒可能会走入一个陷阱,从而提前解救小易。 输入描述:第一行为一个整数n(n ≤ 1000),表示超超一共拥有n个陷阱。第二行有n个整数xi,表示第i个陷阱的横坐标第三行有n个整数yi,表示第i个陷阱的纵坐标保证坐标都在草地范围内。输出描述:输出一个整数,表示小易最少可能多少秒就落入超超的陷阱输入例子1:34 6 81 2 1输出例子1:3''''''解题思路:广度优先搜索算法(bfs) 1、先遍历初始点集合,判断集合中的点上是否有陷阱,若有陷阱,返回0;若无,则把初始点放入已经搜索过的点的集合searched。 利用get_neighbor函数得到初始点集合附近的点,附近的点不能超过边界,也不能在searched中,将函数返回的集合放入待搜索点集合中 2、遍历结束后,如果待搜索点集合非空,则对其进行递归bfs搜索,若递归bfs返回值为-1,表示搜索失败,继续返回-1,若返回值 不是-1,则返回 1+返回值 表示步数 3、若待搜索集合为空,表示所有点都遍历完,未发现陷阱,搜索失败,返回-1''''''代码运行结果:内存超限:您的程序使用了超过限制的内存case通过率为90.00%'''
n = int(input())x_cords = [int(each) for each in input().split()]y_cords = [int(each) for each in input().split()]traps = [(x_cords[i], y_cords[i]) for i in range(n)]def get_neighbor(pos): neighbor = set() if pos[0] + 1 < 1000 and (pos[0]+1, pos[1]) not in searched: neighbor.add((pos[0]+1, pos[1])) if pos[1] + 1 < 1000 and (pos[0], pos[1]+1) not in searched: neighbor.add((pos[0], pos[1]+1)) return neighbordef bfs(current_list): wait_list = set() for each in current_list: if each in traps: return 0 else: searched.add(each) wait_list.update(get_neighbor(each)) if wait_list: temp = bfs(wait_list) if temp != -1: return 1 + temp else: return -1searched = set()init_pos = set()init_pos.add((1, 1))print(bfs(init_pos))
阅读全文
0 0
- 网易2017内推笔试2:解救小易 [python]
- 解救小易----网易2017内推笔试编程题合集(二)
- 网易2017内推笔试2:小易喜欢的单词 [python]
- 网易2017内推笔试2:饥饿的小易 [python]
- 网易笔试:解救小易
- 网易2017内推笔试2:Fibonacci数列 [python]
- 网易2017内推笔试2:不要二 [python]
- 网易2017内推笔试2:幸运的袋子 [python]
- 网易2017内推笔试2:数字游戏 [python]
- 网易2017内推笔试2:混合颜料 [python]
- 网易2017内推笔试2:统计回文 [python]
- 网易笔试编程-解救小易
- 网易 解救小易
- 网易解救小易
- 网易2017内推笔试1:下厨房 [python]
- 网易2017内推笔试1:分田地 [python]
- 网易2017内推笔试1:分苹果 [python]
- 网易2017内推笔试1:合唱团 [python]
- nginx负载均衡的策略
- HTML背景图铺满页面
- js 自定义数据属性
- Speex
- Nagios可用的全部的宏
- 网易2017内推笔试2:解救小易 [python]
- python: argparse库 & 命令行解析工具
- Android ZxingPlus精简的二维码框架
- C++primer 薄片系列之模板
- 破解手机解锁密码
- 使用listfiles()报错:NullPointerException
- ${pageContext.request.contextPath}|request.getContextPath() 项目路径怎么在js(jquery)中获得
- 1050. 螺旋矩阵(25)
- Qt学习------自定义拖放操作