我已经为此苦苦挣扎了几天,在Python中,没有办法对JAVA中的矩阵执行“ int [] []”。我的第一个想法是使用DFS方法耗尽路径。在程序中,我设置了递归路径。但是,无法始终拥有路径,这意味着我必须迭代提供的图中的所有其他节点。我是编程新手,代码可能是WET。
我的想法是:如果当前顶点等于1,请使用DFS耗尽路径并在存在路径时对其进行计数。
如果当前顶点等于0,则找到下一个节点并重复第一步。
这是我的代码(不完整)def dfs_explore(graph, current_vertex, visited_x= None, visited_y = None):
if visited_x is None:
visited_x = []
if visited_x is None:
visited_y = []
stack_x = []
stack_y = []
rows = len(graph)
cols = len(graph[0]) if rows else 0
if not (visited_x and visted_y):
for x in range(cols):
for y in range(rows):
if graph[x][y] != 0:
stack_x.append(x) # the column neighbours
stack_y.append(y)
visited.append(x)
visited.append(y)
path = dfs_explore(graph, graph[x][y], visited_x, visited_y)
if path:
return path
else not path:
stack_x.pop()
stack_y.pop()