zoukankan      html  css  js  c++  java
  • 简单路径搜索

    #include <iostream>
    
    using namespace std;
    
    struct Graph{
        int vertexs;
        int **adj;
    };
    
    struct Edge{
        int v;
        int w;
    };
    
    void GraphInit(Graph* G, int v)
    {
        G->vertexs = v;
        G->adj = new int*[v];
        for (int i = 0; i < v; ++i)
            G->adj[i] = new int[v];
    
        for (int i = 0; i < v; ++i)
            for (int j = 0; j < v; ++j)
                G->adj[i][j] = 0;
    }
    
    void GraphInsertEdge(Graph* G, Edge e)
    {
        G->adj[e.v][e.w] = 1;
        G->adj[e.w][e.v] = 1;
    }
    void GraphPrint(Graph* G)
    {
        for (int i = 0; i < G->vertexs; ++i){
            for (int j = 0; j < G->vertexs; ++j)
                cout << G->adj[i][j] << " ";
            cout << endl;
        }
    }
    
    //简单路径搜索
    static int visited[10] = {0};
    
    int GraphPath(Graph* G, int v, int w)
    {
        int t;
        if (v == w) return 1;
        visited[v] = 1;
        for (t = 0; t < G->vertexs; ++t)
            if (G->adj[v][t] == 1)
                if (visited[t] == 0)
                    if (GraphPath(G, t, w)) return 1;
        return 0;
    }
    
    
    int main()
    {
        Graph G;
        GraphInit(&G, 7);
    
        Edge e[10] = {{0, 5}, {0, 1}, {0, 2}, {0, 6},
                        {1, 2},
                        {2, 3}, {2, 4},
                        {3, 4},
                        {4, 5}, {4, 6}};
    
        for (int i = 0; i < 10; ++i)
            GraphInsertEdge(&G, e[i]);
    
        GraphPrint(&G);
    
        cout << GraphPath(&G, 0, 4) << endl;
    
        return 0;
    }
    
    
  • 相关阅读:
    软件测试培训第9天
    软件培训第8天
    软件测试培训第7天
    软件测试培训第5天
    软件测试培训第6天
    软件测试培训第4天
    软件测试培训第3天
    MySQL复杂用法
    MySQL的基本语法
    VM虚拟机上安装Redhat
  • 原文地址:https://www.cnblogs.com/shamoof/p/4784743.html
Copyright © 2011-2022 走看看