HDU 2181 哈密顿绕行世界问题
Problem Description
Input
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
Source
#include<stdio.h>
struct node
{
int n1,n2,n3;//连接的三个国家,输入即处理成字典序排列,虽然网上没处理的也AC了
bool vis;//是否走过
}country[+];
void chuli(int i)//处理成按字典序排列
{
int c;//中间转换变量
if(country[i].n2<country[i].n1)
{
c=country[i].n1;
country[i].n1=country[i].n2;
country[i].n2=c;
}
if(country[i].n3<country[i].n2)
{
c=country[i].n2;
country[i].n2=country[i].n3;
country[i].n3=c;
}
if(country[i].n2<country[i].n1)
{
c=country[i].n1;
country[i].n1=country[i].n2;
country[i].n2=c;
}
} int m,d;//m是出发的国家,d是线路的序号
int route[+];
void dfs(int n,int r)//n是当前国家的序号,r是走过国家的总数
{
country[n].vis=true;
route[r]=n;
if(r==&&(country[n].n1==m||country[n].n2==m||country[n].n3==m))//从一个城市出发经过每个城市刚好一次后回到出发的城市,满足题目要求
{
d++;
printf("%d: ",d);
for(int i=;i<=;i++)
printf(" %d",route[i]);
printf(" %d\n",m);
return ;
}
if(country[country[n].n1].vis==false)
{
dfs(country[n].n1,r+);
country[country[n].n1].vis=false;
}
if(country[country[n].n2].vis==false)
{
dfs(country[n].n2,r+);
country[country[n].n2].vis=false;
}
if(country[country[n].n3].vis==false)
{
dfs(country[n].n3,r+);
country[country[n].n3].vis=false;
}
}
int main()
{
for(int i=;i<=;i++)
{
scanf("%d%d%d",&country[i].n1,&country[i].n2,&country[i].n3);
chuli(i);
}//输入地图完成
while(scanf("%d",&m),m)
{
d=;
for(int i=;i<=;i++)
country[i].vis=false;
//以上每次要做的清零工作
dfs(m,);
}
return ;
}
HDU 2181 哈密顿绕行世界问题的更多相关文章
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2181 哈密顿绕行世界问题 (DFS)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU - 2181 哈密顿绕行世界问题 dfs图的遍历
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 2181 哈密顿绕行世界问题 dfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2181 只有20个城市,而且每个点的度数恰好是3,也就意味着,对于即将进入环中的点,入度1,出度2,下一个点只有两 ...
- hdu 2181 哈密顿绕行世界问题【DFS】
题目链接 题目大意: Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input ...
- HDU 2181 哈密顿绕行世界问题(DFS)
Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...
- HDU 2181 哈密顿绕行世界问题 (DFS)
题目链接:https://vjudge.net/contest/185350#problem/C 题目大意:一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城 ...
- (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)
题目大意:通俗点讲就是,输出所有从m城市出发,便利所有城市之后又能回到m城市的序列...... 解题思路:DFS 1)用map[][]来存储城市之间的连通情况.用used[]存储某个城市的使用情况(即 ...
- HDOJ 2181 哈密顿绕行世界问题
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- GridView Tab/Enter键插入新行
此段代码是从DevExpress官方回复文件中得到的.可以用来优化GridView的操作体验. /// <summary> /// GridView添加新行 /// </summar ...
- 为知笔记markdown插件安装
Wiz.Editor.md 是一个基于 Editor.md 构建的为知笔记 Markdown 插件. 主要特性 多种样式主题 支持实时预览 支持代码高亮 支持搜索替换 支持ToC目录 Tex数学公式 ...
- Web.config 文件中的 system.webServer
Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置.system.WebServer 是 configuration 节的子级 ...
- Mysql之CentOS初探
1. 卸载mysql 查看CentOS是否已经安装mysql数据库 rpm -qa | grep mysqlrpm -qa | grep MySQL 如果有,则卸载 // --nodeps表示强制rp ...
- 隐藏android中EditText中的下划线以及修改光标颜色
在android开发中 EditTextText是我们经常用到的,我们使用时会有一些小问题,当我们点击输入文字时,EditText中的光标和下划线会变成粉红色. 解决方法很简单,我们只要在EditTe ...
- 自定义报表开发(HTML/XML)
定义报表执行的包或存储过程: --创建包头 CREATE OR REPLACE PACKAGE XXPLM_AARONTEST001 IS PROCEDURE MAIN(errbuf OUT VARC ...
- object model 概述
Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...
- 绿色版的mysql安装配置方式
解压下载好的压缩包 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的原版文件为 # For advice on how to ...
- centos php nginx 添加到service
1. nginx A. # vi /etc/init.d/nginx B. #!/bin/sh # Comments to support chkconfig on RedHat Linux # ch ...
- Linux 服务器系统监控脚本 Shell【转】
转自: Linux 服务器系统监控脚本 Shell - 今日头条(www.toutiao.com)http://www.toutiao.com/i6373134402163048961/ 本程序在Ce ...