Variable change unexpectedly on recursion?

江枫思渺然 提交于 2019-12-02 13:48:13

问题


Context

I'm currently attempting Reddit's /r/dailyprogrammer challenge.

The idea is to find a solution to an ASCII maze. Unfortunately the recursion is working differently than I expected. The program checks if there is space to move to the right, left, below or above the current space. If there is then the space is moved to and the function is entered again with new co-ordinaries. This continues until the end is found.

When the end is found the program exits. If a dead end is found then the recursion will back up to the previous point and check for any more directions, this continues until the end.


Problem

My program works great however the maze draws my lines (represented by '*****') even after the recursion is backed up. I don't know how to explain that so I'll use images to give a better description.

Each new color represents a new path. However I would have expected only the current recursions path to show. For example in this case I would expect only the yellow path to show. Can someone help me understand why all paths remain?


The code

import time
import sys
import os

maze = """\
###############
#S        #   #
### ### ### # #
#   #   #   # #
# ##### ##### #
#     #   #   #
# ### # ### ###
# #   # #   # #
# # ### # ### #
# # # # # #   #
### # # # # # #
#   #   # # # #
# ####### # # #
#           #E#
###############"""

def displayMaze(maze):
    os.system("cls")
    display = ""
    for x in maze:
        for y in x:
            display = display + y
        display = display + "\n"
    print(display)

def findStart(maze):
    #Get the maze start co-ords.
    for x in range(0,len(maze[0])):
        for y in range(0,len(maze)):
            if maze[x][y] == "S":
                return x,y

def findPath(x,y,maze):
    #Look right, left, up and down, If path then move.
        time.sleep(0)
        if maze[y][x+1] == " ":
            newMaze = maze
            newMaze[y][x+1] = "*"
            displayMaze(newMaze)
            findPath(x+1,y,newMaze)
        elif maze[y][x+1] == "E":
            sys.exit("Done")
        if maze[y][x-1] == " ":
            newMaze = maze
            newMaze[y][x-1] = "*"
            displayMaze(newMaze)
            findPath(x-1,y,newMaze)
        elif maze[y][x-1] == "E":
            sys.exit("Done")
        if maze[y+1][x] == " ":
            newMaze = maze
            newMaze[y+1][x] = "*"
            displayMaze(newMaze)
            findPath(x,y+1,newMaze)
        elif maze[y+1][x] == "E":
            sys.exit("Done")
        if maze[y-1][x] == " ":
            newMaze = maze
            newMaze[y-1][x] = "*"
            displayMaze(newMaze)
            findPath(x,y-1,newMaze)
        elif maze[y-1][x] == "E":
            sys.exit("Done")


if __name__ == "__main__":
    maze = maze.split("\n")
    newMaze = []
    for line in maze:
        newMaze.append(list(line))
    x,y = findStart(newMaze)
    findPath(x,y,newMaze)

回答1:


newMaze = maze doesn't copy the list, it just creates another name pointing to the same object. To copy, you should import copy at the top of your program, then do newMaze = copy.deepcopy(maze). (You need a deep copy because maze is a list of lists, so you need to copy not only the outer list, but all the lists inside it too.)

In Python, assignment to a plain name (like blah = ...) never copies anything. If you want a copy, you must make one explicitly. The way to do that depends on what you're copying.



来源:https://stackoverflow.com/questions/24068935/variable-change-unexpectedly-on-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!