#include<stdio.h>
struct
{
int city,pre;
} sq[100];
int jz[50][50];
int qh,qe,n,visited[100];
void out(int qe)//輸出結果
{
if(sq[qe].pre==0)
printf("%d",sq[qe].city);
else
{
out(sq[qe].pre);
printf("--%d",sq[qe].city);
}
}
void createGraph(int n)//創建鄰接矩陣
{
int i,j;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d",&jz[i][j]);
}
void search(int p,int q)//查找
{
int i;
qh=0;
qe=1;
sq[1].city=p;
sq[1].pre=0;
visited[1]=1;
while(qh!=qe) //當隊不爲空
{
qh=qh+1; //結點出隊
for(i=0; i<n; i++)
if(jz[sq[qh].city][i]==1&&visited[i]==0) //如果從城市sq[qh].city可以直接到達城市i,且城市i沒有訪問過
{
qe=qe+1;//結點入隊
sq[qe].city=i;
sq[qe].pre=qh;
visited[i]=1;
if(sq[qe].city==q)
{
printf("基本路徑爲:");
out(qe);
return ;
}
}
}
printf("兩點不連通!\n");
}
int main()
{
int i,p,q;
printf("請輸入頂點個數:");
scanf("%d",&n);
printf("請輸入鄰接矩陣:\n");
createGraph(n);
for(i=0; i<n; i++)
visited[i]=0;
printf("請輸入兩結點:");
scanf("%d%d",&p,&q);
search(p,q);
return 0;
}
/*
0 1 1 1 0 1 0 0
1 0 0 0 0 1 0 0
1 0 0 1 1 0 0 0
1 0 1 0 0 0 1 0
0 0 1 0 0 0 1 1
1 1 0 0 0 0 0 1
0 0 0 1 1 0 0 1
0 0 0 0 1 1 1 0
*/