繁体   English   中英

使用Python中的堆栈在迷宫求解算法中切换X和Y坐标

[英]Switching X and Y Coordinates in Maze Solving Algorithm Using Stack in Python

我正在尝试使用Python 3中的堆栈来解决一个简单的迷宫求解算法。

我找到了一些代码,并添加了一些输出以跟踪发生的事情,但是编写代码的人使用x,y坐标(按此顺序)来表示2d数组。 据我了解,它应该使用y,x代替(对于行然后是列)。 我在遵循算法时遇到了很多麻烦,而不必费心地切换x和y来了解正在发生的事情。

我敢肯定这是一个很小的更改,但是有人可以建议修改以使代码显示并正确跟踪坐标吗? 我不在乎y轴是向上还是向下增加。

我的小迷宫如下所示:

 ***
  **
* **
* G*

代码如下:

# This program traverses a maze using a stack.

from sq import Stack              # import a Stack type from sq.py

MAZE_SIZE = 4                    # define the size of the maze

def PrintMaze(maze):              # an auxilliary function that prints a maze
    for row in range(MAZE_SIZE):
        print(maze[row], end='')
    print()

def InBounds(xxx_todo_changeme):              # an auxillary function that determines if (x,y) is on the maze
    (x,y) = xxx_todo_changeme
    return (0 <= x < MAZE_SIZE) and (0 <= y < MAZE_SIZE)

def Maze(maze, start):          # traverse 'maze' from starting coordinates 'start'
    s = Stack()                  # create a new Stack named s
    s.push(start);               # push the start coordinates onto s
    while not s.isEmpty():       # loop while s is not empty
        print(s.list)
        input("press Enter to continue ")
        (x, y) = s.pop()          # pop a coordinate off s into the tuple (x,y)
        print('Trying position ({}, {})'.format(x,y))
        if InBounds((x,y)):         # if (x,y) is on the maze then
            if maze[x][y] == 'G':    # if (x,y) is the goal then
                s.empty()             # empty the stack because we're done
            elif maze[x][y] == ' ':  # else if (x,y) is an undiscovered coordinate
                print('filling ({}, {})'.format(x,y))
                maze[x] = maze[x][:y] + 'D' + maze[x][y+1:]  # mark (x,y) discovered with 'D'
                PrintMaze(maze);      # print the maze to show progress so far
                s.push((x+1, y))      # push right neighbor onto stack s
                s.push((x, y+1))      # push lower neighbor onto stack s
                s.push((x-1, y))      # push left neighbor onto stack s
                s.push((x, y-1))      # push upper neighbor onto stack s
        else:
            print('Out of bounds.')

# The following can be used to create a maze and traverse it:

import sys
maze = open('maze2.dat', 'r')     # open the file 'maze.dat' for reading
maze = maze.readlines();         # read the file into maze
Maze(maze, (0,0))                 # traverse the maze starting at (0,0)

下线后

input("press Enter to continue ")

您只需输入:

(y, x) = s.pop()

翻转整个代码的坐标

我最近也从事过迷宫求解器的工作...这是我的代码中的示例迷宫和打印功能:

maze=[[ 0 ,0,1,1,0,'S'],
      [ 0 ,0,0,0,1, 0 ],
      ['F',1,0,0,0, 0 ],
      [ 0 ,0,0,0,1, 0 ],
      [ 0 ,1,0,0,0, 0 ]]
## 0 represents empty space, 1 represents a wall
def printmaze(maze):
    for i in maze:
        for j in i:
            print('X' if j==1 else (' ' if j==0 else j),end='')
        print()

同样在您的代码中:

def PrintMaze(maze):              # an auxilliary function that prints a maze
    for row in range(MAZE_SIZE):
        print(maze[row], end='')
    print()

是相同的:

def PrintMaze(maze):
    for row in maze:
        print(row)

为了反转打印,您应该执行以下操作:

for col in range(MAZE_SIZE):
    for row in maze:
        print(row[col],end='')
    print()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM