HDU2181 哈密顿绕行世界问题 —— DFS
题目链接: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
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
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
题解:
有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, );
}
}
HDU2181 哈密顿绕行世界问题 —— DFS的更多相关文章
- HDU2181:哈密顿绕行世界问题(DFS)
		
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
 - HDU 2181 哈密顿绕行世界问题 (DFS)
		
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
 - Hdu2181 哈密顿绕行世界问题                                                                                            2017-01-18 14:46             45人阅读              评论(0)              收藏
		
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
 - HDU - 2181 哈密顿绕行世界问题 dfs图的遍历
		
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
 - HDU2181 哈密顿绕行世界问题
		
解题思路:哈密顿环游世界问题.一道简单的题目,用回溯. #include<cstdio> #include<cstring> #include<algorithm> ...
 - HDU 2181 哈密顿绕行世界问题 dfs 难度:1
		
http://acm.hdu.edu.cn/showproblem.php?pid=2181 只有20个城市,而且每个点的度数恰好是3,也就意味着,对于即将进入环中的点,入度1,出度2,下一个点只有两 ...
 - (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)
		
题目大意:通俗点讲就是,输出所有从m城市出发,便利所有城市之后又能回到m城市的序列...... 解题思路:DFS 1)用map[][]来存储城市之间的连通情况.用used[]存储某个城市的使用情况(即 ...
 - 【HDU - 2181】哈密顿绕行世界问题(dfs+回溯)
		
-->哈密顿绕行世界问题 Descriptions: 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前2 ...
 - HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
		
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
 
随机推荐
- OsCache MemCached EhCache
			
Memcache:分布式内存对象缓存系统,占用其他机子的内存.很多互联网,负载均衡三台(以三台为例)web服务器可以共享一台Memcache的资源.传递的信息以键值对的形式存储.传递的数据要实现序列化 ...
 - 【SPOJ220】Relevant Phrases of Annihilation(后缀数组,二分)
			
题意: n<=10,len<=1e4 思路: #include<cstdio> #include<cstring> #include<string> # ...
 - 杭电1863 畅通project
			
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
 - BUPT复试专题—Python List(2014)
			
题目描述 在Python中,List (列表)是一种非常重要的数据结构.它与C/C++/Java中的 数组有些类似,但支持添加新元素时的动态扩展.在这个问题中,你需要处理如下 的几种对List的操作. ...
 - IE浏览器打不开解决的方法
			
windows 7和windows 8上的IE浏览器打不开.非常可能是权限问题,解决的方法: 点击"開始"-"执行",输入"regedit" ...
 - centos 重新获取IP  hdcp 模式
			
centos 重新获取IP hdcp 模式 dhclient -r →release dhclient →renew
 - Spring Boot 使用Java代码创建Bean并注冊到Spring中
			
从 Spring3.0 開始,添加了一种新的途经来配置Bean Definition,这就是通过 Java Code 配置 Bean Definition. 与Xml和Annotation两种配置方式 ...
 - MRP  Force Reservation的作用
			
生产单根据BOM计算出相应的物料需求,生产领料单stock.picking ( internal moves) Stock.picking使用工作流自动计算库存量,如果库存量够,则使用 test_as ...
 - Linux下的ELF可执行文件的格式解析 (转)
			
LInux命令只是和Kernel一起被编译进操作系统的存在于FS的ELF格式二进制文件,或者权限足够的脚本,或者一个软链 ELF(Executable and Linking Format)是一种对象 ...
 - 双向队列(STL做法)
			
双向队列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 想想双向链表--双向队列的定义差点儿相同,也就是说一个队列的队尾同一 ...