标签:des style blog io ar color sp for strong
Description
假设无向图G采用邻接矩阵存储,设计一个算法,输出图G中从顶点u到v的所有简单路径。
Input
简单路径是指路径上的顶点不重复。第一行为一个整数n,表示顶点的个数(顶点编号为0到n-1),第二行表示顶点u和v的编号,接下来是为一个n*n大小的矩阵,表示图的邻接关系。数字为0表示不邻接,1表示不邻接。
Output
输出图G中从顶点u到v的所有简单路径。
Sample Input
#include<stdio.h> #include<string.h> int map[1100][1100];//存地图 int point; struct a//记录当前位置 { int now; }q[10000]; int start,end;//起止点 int vis[10000];//标记 void print(int h)//打印函数 { for(int i=0;i<=h;i++) printf("%d",q[i].now); printf("\n"); } void DFS(int x)//深度搜索 { if(q[x].now==end){ print(x); return; } for(int i=0;i<point;i++) if(map[i][q[x].now]==1&&vis[i]==0){ q[x+1].now=i; vis[i]=1;//标记 DFS(x+1); vis[i]=0;//标记解除 } } int main() { scanf("%d",&point); scanf("%d%d",&start,&end); for(int i=0;i<point;i++) for(int j=0;j<point;j++) scanf("%d",&map[i][j]); // q[0].front=-1; vis[0]=1; q[0].now=start; DFS(0); return 0; } /* 5 0 3 0 1 0 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 0 1 1 0 1 1 0 */+
标签:des style blog io ar color sp for strong
原文地址:http://www.cnblogs.com/yanglingwell/p/4126413.html