有什么办法可以找到我的宝贝(2)?

我尝试使用以下代码来跟踪并找到宝藏(2)...该程序反而使我“未成功”。

def dfs(current_path):
    # anchor
    row, col = current_path[-1]

    # try all directions one after the other
    for direction in [(row, col + 1), (row, col - 1), (row + 1, col), (row - 1, col)]:
        new_row, new_col = direction
        if (0 <= new_row < num_rows and 0 <= new_col < num_cols
            and matrix[new_row][new_col] == 0 and (new_row, new_col) not in current_path):
            current_path.append((new_row, new_col))
            if dfs(current_path):  # recursive call
                return True
            else:
                current_path.pop()  # backtrack
评论