您的位置:首页 > 其它

【算法】图的深度优先遍历(有向)

2016-03-31 12:19 288 查看
这是小白自己写的,可能里边优化不行,但是能运行的。

其中变量的在函数间的传递一直是我的迷茫点,二维数组的传递更是没有用过,经过一天的摸索,才发现是自己想的太复杂了。。

下边是代码:

#include<iostream>
#include<ctime>
#define NUM 4
static int temp_end = -1, temp_head = -1;
static int n=0;

int Find(int c_1[][NUM],int end_1)//寻找
{

for (int i = 0; i < NUM; i++)
{
if (c_1[end_1][i] == 1)
{

std::cout << n << "\n";
n++;
c_1[end_1][i] = -1;
temp_end = end_1;
temp_head = i;
Find(c_1, i);
}
}
if (temp_end == -1 && end_1 < (NUM-1))
Find(c_1, ++end_1);
return 0;
}

void FindDrive(int c[][NUM])//驱动
{
int end = 0;
Find(c,end);
}

int main()
{
int c_relation[4][4];
for (int i = 0; i < NUM; i++)
for (int j = 0; j < NUM; j++)
c_relation[i][j] = 0;
c_relation[0][1] = 1;
c_relation[0][2] = 1;
c_relation[1][3] = 1;
c_relation[2][1] = 1;
c_relation[3][1] = 1;
c_relation[3][2] = 1;
FindDrive(c_relation);
for (int i = 0; i < NUM; i++)
{
for (int j = 0; j < NUM; j++)
std::cout << c_relation[i][j] << " ";
std::cout << "\n";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: