一个规则的实心十二面体,它的 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

就是走路径的,用个二维数组,储存哪两个城市可以走,在用一个visit数组,储存走过的城市,然后dfs一下就好了;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf(a) scanf("%d",&a)
#define sff(a,b) scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pf printf
#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
int a[25][25],ans=1,visit[21];
int x,y,z;
vector<int>v;
void dfs(int n)
{
if(v.size()==20)//存了20个说明走完了
{
if(a[v[19]][x]==1)
{
pf("%d: ",ans++);
rep(i,0,v.size())
{
pf("%d ",v[i]);
}
pf("%d\n",x);
}
}
rep(i,1,21)
{
if(a[n][i]&&visit[i])
{
a[n][i]=0;
visit[i]=0;//走过标记
v.pb(i);//存入走过的城市,好在后面输出
dfs(i);
visit[i]=1;
v.pop_back();
a[n][i]=1;
}
}
}
int main()
{ mm(a,0);
rep(i,1,21)
{
sfff(x,y,z);
a[i][x]=1;
a[i][y]=1;
a[i][z]=1;
visit[i]=1;
}
while(1)
{
sf(x);
if(!x) return 0;
v.clear();//记得清空
v.pb(x);
visit[x]=0;
dfs(x);
}
}

J - 哈密顿绕行世界问题的更多相关文章

  1. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. HDOJ 2181 哈密顿绕行世界问题

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU2181:哈密顿绕行世界问题(DFS)

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  4. 哈密顿绕行世界问题、n皇后问题

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. 哈密顿绕行世界问题(hdu2181)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  7. HDU2181 哈密顿绕行世界问题 —— DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...

  8. HDU - 2181 C - 哈密顿绕行世界问题(DFS

    题目传送门 C - 哈密顿绕行世界问题 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...

  9. HDU 2181 哈密顿绕行世界问题 (DFS)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. spring mvc 链接 postgresql

    上一篇文章已经分享了搭建springmvc:http://www.cnblogs.com/liqiu/p/4252788.html 这一篇来链接数据库postgresql 1.在pom.xml添加几个 ...

  2. (亲测解决)每次打开excel文件都会出现两个窗口,一个是空白的sheet1,另一个是自己的文档

    版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/sinat_34104446/article/details/80210424 一.问题描述每次打开Exce ...

  3. GDALSetProjection使用的一个注意事项

    GDALSetProjection 简述 GDALSetProjection是用来给GDALDataset设定投影信息(坐标系统)的接口,实际上是GDALDataset::SetProjection这 ...

  4. gcc/g++ disable warnings in particular include files

    当在编译一个大项目的时候,你想打开所有的Warning,但是打开后发现一堆公共库文件都出现了warning报错.此时如果你想忽略公共库头文件中的warning报错,只需要在编译的时候,将公共库头文件的 ...

  5. mongodb浅析

    设计特征: MongoDB 的设计目标是高性能.可扩展.易部署.易使用,存储数据非常方便.其主要功能特性如下. (1)面向集合存储,容易存储对象类型的数据.在MongoDB 中数据被分组存储在集合中, ...

  6. function 与 => 的区别

    在JS中,箭头函数并不是简单的function(){}匿名函数的简写语法糖,实际上,箭头函数和匿名函数有个明显的区别:箭头函数内部的this是词法作用域,在编写函数时就已经确定了.而匿名函数的this ...

  7. Emacs常用基本操作

    按键约定 组合按键 Emacs中大量的按键操作都是各式各样的组合按键(快捷键),下面是几种通常的约定: Ctrl键:表示为C Alt键:表示为M Shift键:表示为S 组合按键:比如向下移动一行的组 ...

  8. 【GMT43智能液晶模块】例程一:ARM驱动LED

    实验原理: 通过STM32的一个GPIO驱动一个红色LED,GPIO为推挽输出模式,采用灌电流 方式与LED连接,基于STemWin人机界面通过按钮控制GPIO高.低电平输出,从而 控制LED亮灭. ...

  9. C#获取起始位置以及添加全局资源字典

    获取起始位置 Path.Combine(AppDomain.CurrentDomain.BaseDirectory); 添加全局资源 string temp = "this is a str ...

  10. Unity5 AssetBundle系列——简单的AssetBundleManager

    一个AssetBundle同时只能加载一次,所以实际使用中一般会伴随着AssetBundle包的管理. 下面是一个简单的AssetBundle管理器,提供了同步和异步加载函数: using Unity ...