python使用map接收输入_python 题目一,给出一张数组map,输入起点和终点,找一通路...

博客围绕一张0、1数组的地图,0表示无障碍,1表示障碍。用户输入起点和终点,采用广度查找法寻找通路。通过遍历循环,利用代码实现从起始点到终点的路径查找,并计算所需步数,若无法到达则给出提示。

问题:

存在一张0,1数组的map,0:无障碍,1:障碍。

用户输入一个起点,和一个终点,寻找一条通路。

分析:

采用广度查找法。

第一次查找:蓝色表示,

第二次查找:红色表示,

按照这个方法来进行遍历循环,得到路径

代码如下:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

'''

这里有一张地图,0表示无障碍,可以通过,

1表示无法通过,需要绕路

输入一个起始点 (0,0)

输入一个终点(3,1)

寻找一条通路,从起始点到终点,一共需要多少步。

'''

map = [ [0,0,1,0],

[0,0,0,0],

[0,1,0,0],

[1,0,0,0]]

row = len(map)

col = len(map[0])

fromX = 0

fromY = 0

endX = 3

endY = 1

print 'from direction(%d,%d)-->(%d,%d):' % (fromX,fromY,endX,endY)

step = 0

direction = [(1,0), (0, 1), (-1, 0), (0, -1)]

moveList = [] #目的标记做过了那些坐标。

newCanMoveList = [] #方便下次遍历是从那些坐标开始遍历。

newCanMoveList.append((fromX, fromY))

while fromX != endX and fromY != endY and len(newCanMoveList) > 0:

prevMoveList = newCanMoveList

newCanMoveList = []

step += 1

for xyItem in prevMoveList:

if xyItem not in moveList:

moveList.append(xyItem)

for dItem in direction:

chgx = xyItem[0] + dItem[0]

chgy = xyItem[1] + dItem[1]

#检查此时是否在地图的边缘,

#检查是否已经走过,或者是障碍物。

if chgx < 0 or chgx >= row or chgy < 0 or chgy >= col :

continue

elif map[chgx][chgy] == 1 or (chgx,chgy) in moveList:

continue

elif chgx == endX and chgy == endY:

fromX = chgx

fromY = chgy

break

else:

newCanMoveList.append((chgx, chgy))

moveList.append((chgx,chgy))

if fromX == endX and fromY == endY:

break

if fromX != endX and fromY != endY:

print "it doesn't have the way from start to end."

else:

print 'it need step %d from start to end.' % step

map_t.fillcolor(r, g, b) # 指定填充颜色 File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2288, in fillcolor color = self._colorstr(args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2696, in _colorstr return self.screen._colorstr(args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1166, in _colorstr raise TurtleGraphicsError("bad color sequence: %s" % str(color)) turtle.TurtleGraphicsError: bad color sequence: (122, 167, 200) Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 686, in eventfun fun() File "E:\turtle.py", line 639, in <lambda> turtle.onkey(lambda: generate_random_maze_display(15, 15), "2") File "E:\turtle.py", line 568, in generate_random_maze_display draw_map(mazeList) File "E:\turtle.py", line 164, in draw_map draw_square(ci, ri, '1') # 绘制墙 File "E:\turtle.py", line 139, in draw_square map_t.fillcolor(r, g, b) # 指定填充颜色 File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2288, in fillcolor color = self._colorstr(args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2696, in _colorstr return self.screen._colorstr(args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1166, in _colorstr raise TurtleGraphicsError("bad color sequence: %s" % str(color)) turtle.TurtleGraphicsError: bad color sequence: (119, 157, 205) Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 686, in eventfun fun() File "E:\turtle.py", line 640, in <lambda> turtle.onkey(lambda: generate_random_maze_display(21, 21), "3") File "E:\turtle.py", line 562, in generate_random_maze_display turtle.title("随机迷宫挑战") File "<string>", line 5, in title turtle.Terminator Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 686, in eventfun fun() File "E:\turtle.py", line 639, in <lambda> turtle.onkey(lambda: generate_random_maze_display(15, 15), "2") File "E:\turtle.py", line 568, in generate_random_maze_display draw_map(mazeList) File "E:\turtle.py", line 164, in draw_map draw_square(ci, ri, '1') # 绘制墙 File "E:\turtle.py", line 134, in draw_square map_t.goto(tx, ty) # 移动到绘图起点(正方形的左上角) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1776, in goto self._goto(Vec2D(x, y)) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 3158, in _goto screen._pointlist(self.currentLineItem), File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 755, in _pointlist cl = self.cv.coords(item) File "<string>", line 1, in coords File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2469, in coords self.tk.call((self._w, 'coords') + args))] _tkinter.TclError: invalid command name ".!canvas" Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 686, in eventfun fun() File "E:\turtle.py", line 640, in <lambda> turtle.onkey(lambda: generate_random_maze_display(21, 21), "3") File "E:\turtle.py", line 565, in generate_random_maze_display draw_game_info() File "E:\turtle.py", line 388, in draw_game_info level_t = turtle.Turtle() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 3816, in __init__ visible=visible) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2557, in __init__ self._update() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2660, in _update self._update_data() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2646, in _update_data self.screen._incrementudc() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1292, in _incrementudc raise Terminator turtle.Terminator Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 686, in eventfun fun() File "E:\turtle.py", line 639, in <lambda> turtle.onkey(lambda: generate_random_maze_display(15, 15), "2") File "E:\turtle.py", line 565, in generate_random_maze_display draw_game_info() File "E:\turtle.py", line 403, in draw_game_info hint_t.goto(0, -R * cell_size / 2 - 40) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1776, in goto self._goto(Vec2D(x, y)) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 3179, in _goto self._update() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2660, in _update self._update_data() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 2646, in _update_data self.screen._incrementudc() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1292, in _incrementudc raise Terminator turtle.Terminator,我报错了,修改后的完整代阿玛
最新发布
08-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值