一道python走迷宫算法题

yipeiwu_com6年前Python基础

前几天逛博客时看到了这样一道问题,感觉比较有趣,就自己思考了下方案顺便用python实现了一下。题目如下:

用一个二维数组表示一个简单的迷宫,用0表示通路,用1表示阻断,老鼠在每个点上可以移动相邻的东南西北四个点,设计一个算法,模拟老鼠走迷宫,找到从入口到出口的一条路径。

如图所示:

先说下我的思路吧:

1、首先用一个列表source存储迷宫图,一个列表route_stack存储路线图,一个列表route_history存储走过的点,起点(0,0),终点(4,4)。

2、老鼠在每个点都有上下左右四种方案可选,需要定义这些方案的执行方法。

3、最后做一个循环,如果当前点不是(4,4)的话就依次执行上下左右四种方法,但是有些限制,比如尝试走过的点不会再尝试走,(0,x)点无法再执行向上的方法等等。

贴一下代码:

# _*_ coding:utf-8 _*_  
route_stack = [[0,0]] 
route_history = [[0,0]] 
source=[[0,0,1,0,1],[1,0,0,0,1],[0,0,1,1,0],[0,1,0,0,0],[0,0,0,1,0]] 
def up(location): 
  #横坐标为0,无法再向上走 
  if location[1] == 0: 
    return False 
  else: 
    new_location = [location[0],location[1]-1] 
    #已经尝试过的点不会尝试第二次 
    if new_location in route_history: 
      return False 
    #碰到墙不走 
    elif source[new_location[0]][new_location[1]] == 1: 
      return False 
    else: 
      route_stack.append(new_location) 
      route_history.append(new_location) 
      return True 
 
def down(location): 
  if location[1] == 4: 
    return False 
  else: 
    new_location = [location[0],location[1]+1] 
    if new_location in route_history: 
      return False 
    elif source[new_location[0]][new_location[1]] == 1: 
      return False 
    else: 
      route_stack.append(new_location) 
      route_history.append(new_location) 
      return True 
 
def left(location): 
  if location[0] == 0: 
    return False 
  else: 
    new_location = [location[0]-1,location[1]] 
    if new_location in route_history: 
      return False 
    elif source[new_location[0]][new_location[1]] == 1: 
      return False 
    else: 
      route_stack.append(new_location) 
      route_history.append(new_location) 
      return True 
 
def right(location): 
  if location[0] == 4: 
    return False 
  else: 
    new_location = [location[0]+1,location[1]] 
    if new_location in route_history: 
      return False 
    elif source[new_location[0]][new_location[1]] == 1: 
      return False 
    else: 
      route_stack.append(new_location) 
      route_history.append(new_location) 
      return True 
lo = [0,0] 
while route_stack[-1] != [4,4]: 
  if up(lo): 
    lo = route_stack[-1] 
    continue 
  if down(lo): 
    lo = route_stack[-1] 
    continue 
  if left(lo): 
    lo = route_stack[-1] 
    continue 
  if right(lo): 
    lo = route_stack[-1] 
    continue 
  route_stack.pop() 
  lo = route_stack[-1] 
print route_stack 

执行结果如下:


题目出处有另一种解题思路,但是我觉得有点烦,自己的这个比较好理解点,实现起来也比较方便。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python绘制直方图和密度图的实例

python绘制直方图和密度图的实例

对于pandas的dataframe,绘制直方图方法如下: //pdf是pandas的dataframe, delta_time是其中一列 //xlim是x轴的范围,bins是分桶个...

Python魔法方法 容器部方法详解

这篇文章主要介绍了Python魔法方法 容器部方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 为了加深印象,也为了以后能够更好...

python自动重试第三方包retrying模块的方法

retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段,retrying提供一个装饰器函数retry,被装饰的函数就会在运行失败的情况下重新执行,默认只要一...

python 将列表中的字符串连接成一个长路径的方法

今天实习公司分配了一个数据处理的任务。在将列表中的字符串连接成一个长路径时,我遇到了如下问题: import os path_list = ['first_directory',...

python检查URL是否正常访问的小技巧

python检查URL是否正常访问的小技巧

今天,项目经理问我一个问题,问我这里有2000个URL要检查是否能正常打开,其实我是拒绝的,我知道因为要写代码了,正好学了点Python,一想,python处理起来容易,就选了pytho...