码迷,mamicode.com
首页 > 其他好文 > 详细

邻接矩阵存储简单路径(swust oj 1070)

时间:2014-11-27 16:14:38      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   sp   for   strong   

Description

假设无向图G采用邻接矩阵存储,设计一个算法,输出图G中从顶点u到v的所有简单路径。

 

Input

简单路径是指路径上的顶点不重复。第一行为一个整数n,表示顶点的个数顶点编号为0n-1,第二行表示顶点uv的编号,接下来是为一个n*n大小的矩阵,表示图的邻接关系。数字为0表示不邻接,1表示不邻接。

 

Output

 

输出图G中从顶点u到v的所有简单路径。

 

Sample Input

 

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
 
 
Sample Output
 
0123
01243
013
03
04213
0423
043
 
 
PE代码:(学校oj有点水,格式键值搞不懂)
 
#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
*/+

 

 

 

 

邻接矩阵存储简单路径(swust oj 1070)

标签:des   style   blog   io   ar   color   sp   for   strong   

原文地址:http://www.cnblogs.com/yanglingwell/p/4126413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!