递归上的变量更改是否意外? [英] Variable change unexpectedly on recursion?

查看:74
本文介绍了递归上的变量更改是否意外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试Reddit的/r/dailyprogrammer挑战.

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

这个想法是找到ASCII迷宫的解决方案.不幸的是,递归的工作方式与我预期的不同.程序检查是否有空间可移动到当前空间的右侧,左侧,下方或上方.如果存在,则将空间移至并使用新的坐标再次输入该功能.这一直持续到找到结尾为止.

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.

我的程序运行良好,但是即使备份了递归,迷宫也会画出我的线条(用'*****'表示).我不知道该怎么解释,所以我将使用图像进行更好的描述.

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?

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)

推荐答案

newMaze = maze不会复制列表,它只是创建指向同一对象的另一个名称.要进行复制,您应该在程序顶部单击import copy,然后执行newMaze = copy.deepcopy(maze). (您需要深度复制,因为maze是一个列表列表,因此不仅需要复制外部列表,还需要复制其中的所有列表.)

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.)

在Python中,分配给纯名称(如blah = ...)绝不会复制任何内容.如果要复制,则必须显式复制一个.这样做的方式取决于您要复制的内容.

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.

这篇关于递归上的变量更改是否意外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆