簡體   English   中英

Python3多次運行相同的function,輸入相同但每次產生不同的輸出

[英]Python3 running the same function with the same input many times but producing different outputs every time

我目前正在嘗試使用 python 解決一個簡單版本的跳棋。 具體來說,我正在嘗試解決 USACO 2008 年 12 月銅牌比賽中的“跳棋”問題。 問題鏈接

我的想法是在每個國王的位置上運行遞歸 dfs function。 但是,我的 dfs function 遇到了一些問題。 當我多次運行我的 dfs function 時,即使使用相同的參數,function 也會產生不同的輸出。 具體來說,它只會在第一時間產生正確的輸出。 我不知道發生了什么,任何幫助將不勝感激,謝謝。 (我正在使用 Python 3.7)

這是我的 dfs function:

def dfs(x, y, n, graph, path, count, visited):
    if str([x+1, y+1]) not in visited:
        visited.add(str([x+1, y+1]))
        if count == 0:
            path += [[x+1, y+1]]
            return path
        if x < 0 or y < 0 or x > n or y > n:
            return path
        path += [[x+1, y+1]]
        try:
            if graph[x+1][y+1] == "o":
                graph[x+1][y+1] = "+"
                return dfs(x+2, y+2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x+1][y-1] == "o":
                graph[x+1][y-1] = "+"
                return dfs(x+2, y-2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x-1][y+1] == "o":
                graph[x-1][y+1] = "+"
                return dfs(x-2, y+2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x-1][y-1] == "o":
                graph[x-1][y-1] = "+"
                return dfs(x-2, y-2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        return path

這是我如何調用我的 dfs function:

print(dfs(7, 2, n, grid.copy(), [], count, set()))
print(dfs(7, 2, n, grid.copy(), [], count, set()))
print(dfs(7, 2, n, grid.copy(), [], count, set()))

這是我得到的 output:

在此處輸入圖像描述

這是我的完整代碼:

n = int(input())
grid = []
for i in range(n):
    grid.append(list(input().rstrip()))
def dfs(x, y, n, graph, path, count, visited):
    if str([x+1, y+1]) not in visited:
        visited.add(str([x+1, y+1]))
        if count == 0:
            path += [[x+1, y+1]]
            return path
        if x < 0 or y < 0 or x > n or y > n:
            return path
        path += [[x+1, y+1]]
        try:
            if graph[x+1][y+1] == "o":
                graph[x+1][y+1] = "+"
                return dfs(x+2, y+2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x+1][y-1] == "o":
                graph[x+1][y-1] = "+"
                return dfs(x+2, y-2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x-1][y+1] == "o":
                graph[x-1][y+1] = "+"
                return dfs(x-2, y+2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        try:
            if graph[x-1][y-1] == "o":
                graph[x-1][y-1] = "+"
                return dfs(x-2, y-2, n, graph, path, count-1, visited)
        except IndexError as e: pass
        return path

count = 0
Ks = []
for x in range(n):
    for y in range(n):
        if grid[x][y] == "K":
            Ks.append([x, y])
        if grid[x][y] == "o":
            count += 1
print(dfs(7, 2, n, grid.copy(), [], count, set()))
print(dfs(7, 2, n, grid.copy(), [], count, set()))
print(dfs(7, 2, n, grid.copy(), [], count, set()))

.copy()列表方法僅適用於列表的一個“層”。 由於grid是列表的列表,因此如果您更改副本,原始內容仍會更改。

例如,在 Python 控制台中嘗試

>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> b = a.copy()
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b[0][0] = 5
>>> a
[[5, 2, 3], [4, 5, 6], [7, 8, 9]]

你看到a已經改變,盡管b被設置為a.copy() 您將需要制作某種形式的“雙重”副本。

或者,使用copy模塊中的deepcopy function:

>>> from copy import deepcopy
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> b = deepcopy(a)
>>> b[0][0] = 5
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM