HDU-2181 哈密顿绕行世界问题
直接DFS,不用加剪枝。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <iostream> #include <fstream> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #define rep(i, l, r) for(int i=l; i<=r; i++) #define down(i, l, r) for(int i=l; i>=r; i--) #define maxn 5678 #define MAX 1<<30 using namespace std; int m, e[21][4], st[21], n, t; bool b[21]; void Search(int x, int n) { if (n == 21) { printf("%d: ", ++t); rep(i, 1, 20) printf("%d ", st[i]); printf("%d\n", m); return; } if (n == 20) { st[n] = x; rep(i, 1, 3) if (e[x][i] == m) Search(m, n+1); return; } b[x] = true; st[n] = x; rep(i, 1, 3) if (!b[e[x][i]]) Search(e[x][i], n+1); b[x] = false; } int main() { rep(i, 1, 20) scanf("%d%d%d", &e[i][1], &e[i][2], &e[i][3]); rep(i, 1, 20) sort(e[i]+1, e[i]+4); scanf("%d", &m); while (m) { t = 0; Search(m, 1); scanf("%d", &m); } return 0; }