python(向函数传递参数)

2024-10-01 07:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python并不陌生,但是我遇到了这个问题,这让我很困惑。你知道吗

所以我用A*解决了迷宫跑步机的问题,然后找到了给定维度下最难的迷宫。为此,我创建了一个名为generateHardMaze()的函数,该函数是从main函数调用的,并具有newMaze属性。你知道吗

现在事情变得很奇怪,当我在while循环中的if条件中更改newMaze的值时,hardMaze的值会在代码没有进入第二个if条件的情况下更改。我不确定为什么会发生这种情况,希望有人能帮我。你知道吗

我使用pycharm作为我的IDE和python3.6.*如果这有什么不同的话。你知道吗

我肯定这不是oops的工作方式,但我认为这是python的东西。有人遇到过这样的事吗?如果是,请表示同情。你知道吗

提前谢谢。你知道吗

def solveMazeAManH(newMaze,rows,cols):
    startTime = time.time()

    backTrackPriority = []

    setup_cells(rows, cols)

    # start and end points of the maze
    start = (0, 0)
    end = (rows - 1, cols - 1)

    current = start

    print("The path to be take is: ")

    print(current)

    frinLength = 0

    # traversing the neighbours
    while current != end:

        unvisited.remove(current)

        neighboursDFSandA(newMaze, current, rows, cols)

        heuristic = calManhattanDis(current, end)  # finding the heuristic for every traversal

        try:
            if not currentNeighbours:

                if not backTrackPriority:
                    print("No path available!")
                    return 0
                else:
                    while not currentNeighbours:
                        current = nextPopMan(backTrackPriority, end)
                        backTrackPriority.remove(current)
                        neighboursDFSandA(newMaze, current, rows, cols)

            neighbor = leastPathChildMan(heuristic, current, end)
            backTrackPriority.append(current)
            current = neighbor
            print(current)
            frinLength += 1

        except:
            print("No path Found!")
            return 0

    return frinLength

    endTime = time.time()

    print("The time taken to solve the maze using A* with manhattan distance: ")
    print(startTime - endTime)


def generateHardMaze(newMazes):

    rows = len(newMazes)
    cols = len(newMazes[0])

    hardMaze = newMaze

    print("Solving the original maze!")
    fringLength = solveMazeAManH(newMazes, rows, cols)
    print("Creating new harder Maze:")

    pFlag = True
    pCout = 0

    while pFlag:

        count = 0
        flag = True

        while flag:

            point = choice(setup_cells(rows, cols))

            if (newMazes[point[0]][point[1]] == 1):
                newMazes[point[0]][point[1]] = 0
            else:
                newMazes[point[0]][point[1]] = 1

            if (fringLength < solveMazeAManH(newMazes, rows, cols)):
                print("Harder Maze--------------------")
                hardMaze = newMaze
                fringLength = solveMazeAManH(newMazes, rows, cols)
                count = 0

            else:
                count += 1
                if count >= 10:
                    flag = False

        print("one")
        newMazes = creatMaze(rows)
        pCout += 1
        if pCout >= 100:
            pFlag = False
    print(hardMaze)

Tags: theiftimecurrentrowsendpointprint