【发布时间】:2019-07-21 20:12:06
【问题描述】:
我想编写一个代码,用于在邻接矩阵形式的给定有向图中查找固定长度的循环
bool check(int vertex,int current_vertex, int k, int** graph , int n) {
if (k == 0)
return (vertex == current_vertex);
for (int i = 0; i < n; i++) {
if (graph[current_vertex][i] == 1) {
graph[current_vertex][i] = 0;
if (check(vertex, i, k - 1, graph, n)) return true;
graph[vertex][i] = 1;
}
}
return false;
}
从 main 调用函数:
for (int i = 0; i < n; i++) {
cycle = check(i,i,k,graph,n);
if (cycle) break;
}
cout << (cycle?"TRUE":"FALSE");
我的输入如下所示,只有一个循环,正如预期的那样,'5' 为真,'1'、'2'、'4' 为假,但对于 '3' 也为真。我错过了什么?
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0
【问题讨论】:
-
我错过了什么? -- A minimal reproducible example.