哈密顿绕行世界问题
题目链接:https://vjudge.net/problem/HDU-2181#author=0
思路:简单搜索,直接dfs即可
#include <bits/stdc++.h> using namespace std; typedef long long ll; int a[30][30],b[30]; int book[30]; int v=0; void dfs(int x,int y,int m) { if(y==21) { if(a[x][m]==1) { printf("%d: ",++v); for(int i=1;i<=20;i++) cout<<b[i]<<" "; cout<<m<<endl; } return; } for(int i=1;i<=20;i++) { if(book[i]==1||a[x][i]==0) continue; book[i]=1; b[y]=i; dfs(i,y+1,m); book[i]=0; } } int main() { for(int i=1;i<=20;i++) { int x,y,z; cin>>x>>y>>z; a[i][x]=a[i][y]=a[i][z]=1; } int m; while(~scanf("%d",&m)) { memset(book,0,sizeof(book)); if(m==0) break; v=0; book[m]=1; b[1]=m; dfs(m,2,m); } }