python(将参数传递给函数)

时间:2019-02-16 13:21:00

标签: python function

我对python并不是很陌生,但是遇到了这个让我感到困惑的问题。

因此,我使用A *解决了迷宫问题,然后找到了给定尺寸的最困难的迷宫。为此,我创建了一个名为generateHardMaze()的函数,该函数从主函数调用,并带有一个属性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)

0 个答案:

没有答案