题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181

哈密顿绕行世界问题

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5132    Accepted Submission(s): 3091

Problem Description
一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。 
 
Input
前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出.
 
Output
输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output
 
Sample Input
2 5 20
1 3 12
2 4 10
3 5 8
1 4 6
5 7 19
6 8 17
4 7 9
8 10 16
3 9 11
10 12 15
2 11 13
12 14 20
13 15 18
11 14 16
9 15 17
7 16 18
14 17 19
6 18 20
1 13 19
5
0
 
Sample Output
1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5
2: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5
3: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5
4: 5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5
5: 5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5
6: 5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5
7: 5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5
8: 5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5
9: 5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5
10: 5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5
11: 5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5
12: 5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5
13: 5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5
14: 5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5
15: 5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5
16: 5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5
17: 5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5
18: 5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5
19: 5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5
20: 5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5
21: 5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5
22: 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5
23: 5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5
24: 5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5
25: 5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5
26: 5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5
27: 5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5
28: 5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5
29: 5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5
30: 5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5
31: 5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5
32: 5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5
33: 5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5
34: 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5
35: 5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5
36: 5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5
37: 5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5
38: 5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5
39: 5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5
40: 5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5
41: 5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5
42: 5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5
43: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5
44: 5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5
45: 5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5
46: 5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5
47: 5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5
48: 5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5
49: 5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5
50: 5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5
51: 5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5
52: 5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5
53: 5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5
54: 5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5
55: 5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5
56: 5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5
57: 5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5
58: 5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5
59: 5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5
60: 5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5
 
Author
Zhousc
 
Source

题解:

有20个点,每个点与3个点连接。如果使用回溯的话,操作次数是:2^20  约等于1e6(实际上要小),所以可以直接dfs回溯。

写法一:由于要回到初始点,所以要走21个点,且vis过的点还要进去试试:是不是最后一个点且为初始点。;

写法二:vis过的点不再访问,当走到第20点时,如果第20个点与起始点相连,那么就能回到起始点。

写法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int g[MAXN][];
int vis[MAXN], path[MAXN], cnt, m; void dfs(int u, int k)
{
if(k==+) //到了最后一个点
{
if(u==m) //回到了出发点
{
printf("%d: ", ++cnt);
for(int i = ; i<=; i++)
printf("%d ", path[i]);
printf("%d\n", m);
}
return;
}
if(vis[u]) return; //没有到达最后一个点,并且vis过的,都要退出 vis[u] = ;
path[k] = u;
for(int i = ; i<; i++) //因为要回到出发点,所以vis过的也要再试一试:是不是最后一个点且是出发点
dfs(g[u][i], k+);
vis[u] = ;
} int main()
{
for(int i = ; i<=; i++)
{
for(int j = ; j<; j++)
scanf("%d",&g[i][j]);
sort(g[i], g[i]+); //保证按字典序输出
} while(scanf("%d",&m) && m)
{
ms(vis, );
cnt = ;
dfs(m, );
}
}

写法二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int g[MAXN][];
int vis[MAXN], path[MAXN], cnt, m; void dfs(int u, int k)
{
path[k] = u; //记录路径
if(k==) //到了倒数第二个点
{
for(int i = ; i<; i++) //如果与初始点相连,那么就可以回到初始点
if(g[u][i]==m)
{
printf("%d: ", ++cnt);
for(int i = ; i<=; i++)
printf("%d ", path[i]);
printf("%d\n", m);
}
return;
} vis[u] = ;
for(int i = ; i<; i++)
if(!vis[g[u][i]])
dfs(g[u][i], k+);
vis[u] = ;
} int main()
{
for(int i = ; i<=; i++)
{
for(int j = ; j<; j++)
scanf("%d",&g[i][j]);
sort(g[i], g[i]+); //保证按字典序输出
} while(scanf("%d",&m) && m)
{
ms(vis, );
cnt = ;
dfs(m, );
}
}

最新文章

  1. CSS3 Animation制作飘动的浮云和星星效果
  2. 为现有图像处理程序添加读写exif的功能
  3. HDU 5113 dfs剪枝
  4. 初学File类
  5. 通过bind实现activity与service的交互
  6. JAX-RS入门 一 :基础
  7. C#函数运行超时则终止执行(任意参数类型及参数个数通用版)
  8. Sublime Text 超好用的侧栏插件SideBarEnhancements
  9. ruby条件控制结构
  10. C# RSA
  11. linux磁盘限额配置:quota命令
  12. 美国vps哪个比较好,vps国内访问速度最快!
  13. Cocos2D:塔防游戏制作之旅(七)
  14. 【C语言编程练习】7.2动态数列排列
  15. 数据的描述性分析_R
  16. 数据库日志redo和undo
  17. 内存(RAM或ROM)和FLASH存储的真正区别总结
  18. Ajax事件,方法
  19. JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)
  20. 升级pip后出现 ImportError: cannot import name main

热门文章

  1. 页面中用Context.Handler传递
  2. BZOJ1902: Zju2116 Christopher
  3. Cards BZOJ 1004
  4. h5表单验证的css和js方法
  5. chromedriver错误信息提示
  6. Python入门--7--处理数据时学习到的东西
  7. electron 编译成exe
  8. codevs——2370 小机房的树
  9. 机器学习优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)
  10. [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)