缺点十分明显,十分占空间。
代码如下:
//图遍历不重复路径输出bfs版 #include<iostream> #include<queue> using namespace std; int minn = 99999999; int map[107][107], n, m, endd; struct point { int num, step;//num为储存当前数字,step储存dis数量 int ss[10] = { -1 };//标记 int dis[10] = { -1 };//储存答案 }; int main() { scanf("%d%d%d", &n, &m, &endd); for (int i = 1;i <= n;i++) for (int j = 1;j <= n;j++) { if (i == j) map[i][j] = 0; map[i][j] = minn; } int a, b; for (int i = 1;i <= m;i++) { scanf("%d%d", &a, &b); map[a][b] = 1; } queue<point> r; point aa, bb; aa.num = 1; aa.step = 0; aa.ss[1] = 1; aa.dis[aa.step] = 1; r.push(aa); while (!r.empty()) { aa = r.front(); r.pop(); if (aa.num == endd) { printf("%d\n", aa.step); for (int k = 0;k <= aa.step;k++) printf("%d ", aa.dis[k]); puts(""); } for (int i = 1;i <= n;i++) { if (aa.ss[i]!=1 && map[aa.num][i] != minn) { bb = aa; bb.num = i; bb.step = aa.step + 1; bb.ss[i] = 1; bb.dis[aa.step + 1] = i; r.push(bb); } } } }