careercup4.2

来源:互联网 发布:围棋比赛编排软件 编辑:程序博客网 时间:2024/05/29 15:31

看连通性当然深搜广搜都可以,当然深搜代码更简单。

/*Given a directed graph, design an algorithm to find out whether there is a route be -tween two nodes*/#include <iostream>#include <assert.h>#define N 6using namespace std;bool dfs(int g[][N], int s,  int t, int v[]){assert( s < N && s >= 0);assert( t < N && t >= 0);if(g[s][t]) return true;for(int i =0; i<N; i++)if(g[s][i] && !v[i]){v[i] = 1;return dfs(g,i,t,v);}return false;}int main(){int g[N][N]={{0,1,1,0,0,0},{0,0,0,1,0,0},{0,0,0,0,1,1},{0,0,0,0,0,1},{0,0,0,1,0,0},{0,0,0,0,0,0}};int v[N] = {0};cout<<dfs(g,1,5,v)<<endl;cout<<dfs(g,1,4,v)<<endl;}


原创粉丝点击