//广度优先算法
/*
#include<iostream>
using namespace std;
int main()
{
int i,j,n,m,a,b,head,tail,cur;
int e[101][101],queue[101],book[101];
cin>>n>>m;//n为点数,m为路径个数
for(i = 1;i <= n;i++)
for(j = 1;j <= n;j++)
{
if(i == j)
e[i][j] = 0;
else
e[i][j] = 999;
}
for(i = 1;i <= m;i++)
{
cin>>a>>b;
e[a][b] = 1;
e[b][a] = 1;
}
//输出数组e表
for(i = 1;i <= n;i++)
{
for(j = 1;j <= n;j++)
cout<<e[i][j]<<" ";
cout<<endl;
}
head = 1;
tail = 1;
queue[tail] = 1;
book[1] = 1;
tail++;
while(head < tail && tail <= n)
{
cur = queue[head];
for(i = 1;i <= n;i++)
{
if(book[i] == 0 && e[cur][i] == 1)
{
queue[tail] = i;
tail++;
book[i] = 1;
}
if(tail > n)
break;
}
head++;
}
for(i = 1;i <= n;i++)
cout<<queue[i]<<endl;
return 0;
}
*/
//深度优先算法
/*
#include<iostream>
using namespace std;
int e[101][101],book[101],n,sum = 0;
void dfs(int cur)
{
int i,sum;
cout<<cur<<endl;
sum++;
if(sum == n)
return;
for(i = 1;i <= n;i++)
{
if(e[cur][i] == 1 && book[i] == 0)
{
book[i] = 1;
dfs(i);
}
}
return;
}
int main()
{
int i,j,m,a,b;
cin>>n>>m;//n为点数,m为路径个数
for(i = 1;i <= n;i++)
for(j = 1;j <= n;j++)
{
if(i == j)
e[i][j] = 0;
else
e[i][j] = 999;
}
for(i = 1;i <= m;i++)
{
cin>>a>>b;
e[a][b] = 1;
e[b][a] = 1;
}
book[1] = 1;
dfs(1);
return 0;
}
*/
|