//邻接矩阵存储简单路径

#include <iostream>
#include <cstring>
using namespace std;

struct graph {
    int data;
    graph* next;
};

int t[100];   //记录所走的路径
int book[100];  //用于标记


void dfs(graph Graph[], int b, int e, int pos)    //pos表示数量   b为起点  e为终点
{ 
    t[pos] = b;  //将当前所在位置存入数组
    if (book[b] == 0)   //用于存第一个放入的b 即将起点标记
    {
        book[b] = 1;
    }
    if (b == e)   //到达终点
    {
        for (int i = 0; i <= pos; i++)
        {
            cout << t[i];
        }
        //结束
        cout <<  endl;
        return;
    }
    graph* p = &Graph[b];
    while (p->next != NULL)   //一直循环查找
    {
        if (book[p->next->data] == 0)
        {
            book[p->next->data] = 1;
            dfs(Graph, p->next->data, e, pos+1);
            book[p->next->data] = 0;
        }
        p = p->next;
    }


}

int main()
{
    memset(book, 0, sizeof(book));
    memset(t, -1, sizeof(t));
    int n;
    cin >> n;
    int b, e;
    cin >> b >> e;

    graph Graph[100];
    for (int i = 0; i < n; i++)
    {
        Graph[i].next = NULL;
        Graph[i].data = i;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int temp;
            cin >> temp;
            if (temp != 0)
            {
                graph* temp = &Graph[i];
                while (temp->next != NULL)
                {
                    temp = temp->next;
                }
                temp->next = new graph;
                temp->next->data = j;
                temp->next->next = NULL;
            }
        }
    }

    dfs(Graph, b, e, 0);

    return 0;
}
最后修改:2019 年 04 月 15 日 11 : 04 PM
如果觉得我的文章对你有用,请随意赞赏