一道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中以绝对路径或者相对路径导入文件的方法

1、在Python中以相对路径或者绝对路径来导入文件或者模块的方法 今天在调试代码的时候,程序一直提示没有该模块,一直很纳闷,因为我导入文件一直是用绝对路径进行导入的。按道理来讲是不会出...

Python入门篇之正则表达式

 正则表达式有两种基本的操作,分别是匹配和替换。 匹配就是在一个文本字符串中搜索匹配一特殊表达式; 替换就是在一个字符串中查找并替换匹配一特殊表达式的字符串。   1...

python微信跳一跳系列之棋子定位像素遍历

python微信跳一跳系列之棋子定位像素遍历

前言 在前几篇博客中,分别就棋子的颜色识别、模板匹配等定位方式进行了介绍和实践,这一篇博客就来验证一下github中最热门的跳一跳外挂中采用的像素遍历的方法。 方法说明 像素遍历的实质依...

python下载库的步骤方法

python下载库的步骤方法

python怎么下载库? pip安装是python中最简单的一种安装第三方库的模式,要使用pip在线安装,我们要保证两个基本条件,分别是: 1. 要安装的机器可以连通外网 2. 知道第三...

详解Python time库的使用

详解Python time库的使用

一、时间获取函数 >>> import time >>> time.time() 1570530861.740123 >>> t...