一道python走迷宫算法题

yipeiwu_com5年前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 OS模块实例详解

本文实例讲述了Python OS模块。分享给大家供大家参考,具体如下: os模块 在自动化测试中,经常需要查找操作文件,比如查找配置文件(从而读取配置文件的信息),查找测试报告等等,经常...

利用Python校准本地时间的方法教程

利用Python校准本地时间的方法教程

1. 概念 1.1 基本概念 时间,对于我们来说很重要,什么时候做什么?什么时候发生什么?没有时间的概念,生活就乱了。 在日常的运维当中,我们更关注告警的时间:什么时候发生、什么事故...

python 画二维、三维点之间的线段实现方法

python 画二维、三维点之间的线段实现方法

如下所示: from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt # 打开画图窗口1,在...

python 自动去除空行的实例

code 原文档 1.txt : Hello Nanjing 100 实现代码: file_ = "1.txt" r_file = open(file_, "r"...

python批量替换页眉页脚实例代码

简介 本文分享的实例代码主要通过python语言实现批量替换页眉页脚的操作功能,具体如下。 代码 #!/usr/bin/env python # -*- coding: utf-8...