Python 解决一个简单的迷宫问题 在线等

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 解决一个简单的迷宫问题 在线等相关的知识,希望对你有一定的参考价值。

具体是什么情况呢

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

matrix = []
row, col = 0, 0

def LoadData():
global matrix
file = open("maze.txt")
lines = file.readlines()
for line in lines:
matrix.append(line.strip())

def Init():
r = len(matrix)
c = len(matrix[0])
global row, col
for cc in xrange(c):
for rr in xrange(r):
if matrix[rr][cc] in ('O', 'F'):
row, col = rr, cc
OutputPos()
return

def OutputPos():
print "You are at position (%d, %d)" % (row, col)

def Move(d):
global row, col
m = (0, 0)
if d == 'N':
m = (-1, 0)
elif d == 'E':
m = (0, 1)
elif d == 'S':
m = (1, 0)
elif d == 'W':
m = (0, -1)

if row + m[0] >= 0 and row + m[0] < len(matrix) and col + m[1] >= 0 and col + m[1] < len(matrix[0]) and matrix[row + m[0]][col + m[1]] in ('O', 'F'):
row = row + m[0]
col = col + m[1]
OutputPos()
else:
print "You can't go in that direction"

def Input():
cmd = raw_input("Command: ")
cmd = cmd.strip()
if cmd == '?':
print '''\
? - Help.
N - move North one square.
S - move South one square.
E - move East one square.
W - move West one square.
R - Reset to the beginning.
B - Back up a move.
L - List all possible legal directions from the current position.
Q - Quit.'''
OutputPos()
elif cmd in ('N', 'E', 'S', 'W'):
Move(cmd)
if matrix[row][col] == 'F':
print "Congratulations - you made it"
elif cmd == 'L':
l = []
if row - 1 >= 0 and matrix[row - 1][col] in ('O', 'F'):
l.append('N')
if col + 1 < len(matrix[0]) and matrix[row][col + 1] in ('O', 'F'):
l.append('E')
if row + 1 < len(matrix) and matrix[row + 1][col] in ('O', 'F'):
l.append('S')
if col - 1 >= 0 and matrix[row][col - 1] in ('O', 'F'):
l.append('W')
for i, x in enumerate(l):
if i > 0:
print ",",
print x,
print ""
elif cmd == 'R':
Init()
elif cmd == 'Q':
if raw_input("Are you sure you want to quit? [y] or n:").strip() == 'y':
return False
else:
print "Invalid Command:", cmd
OutputPos()
return True

if __name__ == "__main__":
LoadData()
Init()
while Input():
pass
参考技术A 问题补充:大概能有一页的样子 谢谢了 =这个是某次应求帮人写的程序#调用上面定于的sparse方法。其实简单的处理用不着这么做的……单纯为了扩展性,cQupyI

java实现简单二维迷宫

这次是改良版本。

将地图封装,老鼠封装。是对Java基础的一个练习吧。

这次实现也遇到了一些问题。主要是栈。封装的mouse类中有成员变量i,j代表了老鼠的坐标。将mouse类对象m入栈的时候,总是入栈的是一个对象m的参考。你再次对m进行修改的时候栈里面的内容也全部变成m的内容。

解决方案:再写一个函数,在函数内部构建一个mouse类对象mtemp,将m的成员变量i,j先赋值给int型变量a,b。再将a,b赋值给mtemp的i,j。再将mtemp入栈。就解决了。

 

接下来附上代码,仅供参考:

  1 //map.java
  2 
  3 package test;
  4 
  5 import java.util.Stack;
  6 
  7 public class map {
  8     int [][] maze={
  9             {2,2,2,2,2,2,2,2,2},
 10             {2,2,2,2,2,2,2,2,2},
 11             {2,0,0,0,0,0,0,2,2},
 12             {2,2,0,2,0,2,0,2,2},
 13             {2,2,0,0,2,0,2,2,2},
 14             {2,2,2,0,2,0,2,2,2},
 15             {2,2,0,0,0,0,0,2,2},
 16             {2,2,2,2,2,2,0,2,2},
 17             {2,2,2,2,2,2,2,2,2}
 18     };    //在地图外面加一圈围墙,防止数组越界。
 19     
 20     int [][] posi={
 21             {0,0,0,0,0,0,0,0,0},
 22             {0,0,0,0,0,0,0,0,0},
 23             {0,0,0,0,0,0,0,0,0},
 24             {0,0,0,0,0,0,0,0,0},
 25             {0,0,0,0,0,0,0,0,0},
 26             {0,0,0,0,0,0,0,0,0},
 27             {0,0,0,0,0,0,0,0,0},
 28             {0,0,0,0,0,0,0,0,0},
 29             {0,0,0,0,0,0,0,0,0}
 30     };  
 31     
 32     Stack<mouse> stack=new Stack<mouse>();
 33     
 34     void createpush(mouse m,Stack<mouse> stack)
 35     {
 36         mouse mtemp=new mouse();
 37         int a=m.geti();
 38         int b=m.getj();
 39         mtemp.seti(a);
 40         mtemp.setj(b);
 41         stack.push(mtemp);
 42     }
 43     
 44     void findpath(mouse m)
 45     {
 46         posi[m.geti()][m.getj()]=1;
 47         
 48         createpush(m,stack);
 49         
 50         while(!stack.empty())
 51         {
 52             if(m.geti()==7&&m.getj()==6)
 53                 break;
 54             if(maze[m.geti()][m.getj()+1]==0&&posi[m.geti()][m.getj()+1]==0)   //向右探索
 55             {
 56                 posi[m.geti()][m.getj()+1]=1;
 57                 m.setj(m.getj()+1);
 58                 createpush(m,stack);
 59                 //m.setj(m.geti()+1);
 60                 //stack.push(m.geti()+‘ ‘+m.getj());
 61             }
 62             else if(maze[m.geti()+1][m.getj()]==0&&posi[m.geti()+1][m.getj()]==0)  //向下探索
 63             {
 64                 posi[m.geti()+1][m.getj()]=1;
 65                 m.seti(m.geti()+1);
 66                 createpush(m,stack);
 67                 
 68             }
 69             else if(maze[m.geti()-1][m.getj()]==0&&posi[m.geti()-1][m.getj()]==0)  //向上探索
 70             {
 71                 posi[m.geti()-1][m.getj()]=1;
 72                 m.seti(m.geti()-1);
 73                 createpush(m,stack);
 74                 
 75             }
 76             else if(maze[m.geti()][m.getj()-1]==0&&posi[m.geti()][m.getj()-1]==0)    //向左探索
 77             {
 78                 posi[m.geti()][m.getj()-1]=1;
 79                 m.setj(m.getj()-1);
 80                 createpush(m,stack);
 81                 
 82             }
 83             else
 84             {
 85                 stack.pop();       //走到死路,就将栈中该位置的点 出栈。
 86                 if(stack.empty())
 87                 {
 88                     break;
 89                 }
 90                 m.seti(stack.peek().geti());       //peek()取栈顶元素。
 91                 m.setj(stack.peek().getj());
 92             }
 93         }
 94     }
 95     
 96     void print(Stack<mouse> stack)
 97     {
 98         Stack<mouse> stacktemp=new Stack<mouse>();
 99         mouse mtemp=new mouse();
100 
101         while(!stack.empty())   //栈反转。
102         {
103             mtemp=(mouse) stack.pop();
104             stacktemp.push(mtemp);
105         }
106         
107         while(!stacktemp.empty())   // 将反转后的栈,挨个出栈,这就是老鼠的轨迹。
108         {
109             System.out.print(stacktemp.peek().geti()-1);
110             System.out.print(" ");
111             System.out.println(stacktemp.peek().getj()-1);
112             stacktemp.pop();
113         }
114     }
115 }
 1 //mouse.java
 2 
 3 package test;
 4 
 5 public class mouse {
 6     
 7     mouse(int i,int j)
 8     {
 9         this.i=i;
10         this.j=j;
11     }
12     mouse()
13     {
14         this.i=0;
15         this.j=0;
16     }
17     
18     void seti( int a)
19     {
20         this.i=a;
21     }
22     void setj(int b)
23     {
24         this.j=b;
25     }
26     
27     int geti()
28     {
29         return i;
30     }
31     int getj()
32     {
33         return j;
34     }
35 
36     private int i,j;
37 }
 1 //test.java
 2 
 3 package test;
 4 
 5 public class test {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9 
10         map maze=new map();
11         mouse m=new mouse(2,1);
12         
13         maze.findpath(m);
14         maze.print(maze.stack);
15         
16     }
17 
18 }

 

这次的练习,虽然很枯燥,但是我却收获了很多,一点点进步吧。

以上是关于Python 解决一个简单的迷宫问题 在线等的主要内容,如果未能解决你的问题,请参考以下文章

python迷宫问题深度优先

用python制作迷宫图

今天大佬带你做一个Python 小项目制作一个迷宫游戏附带源码

在我尝试使用 python 解决的迷宫问题中,显示类型错误 int 对象在老鼠中不可下标

数据结构小设计--(迷宫问题Python版本)

迷宫求解程序的回溯逻辑错误