哈密顿绕行世界问题(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): 624    Accepted Submission(s): 380

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

ECJTU 2008 Summer Contest

解析:

1:题意:给出20个城市吗,从给定的某个城市出发,要求不重复地走过每一个城市,最终回到起点,按字典序输出所有情况;

2:

思路:这是一道赤裸裸的深搜题:

从深搜的路径可分为两种:

法一”三根筋“搜:因为题目给出每个城市相邻的三城市,故而,每次从当前城市搜索周围三个可走的城市

法二”一根筋“,就是将可行走的道路统统标记为”1“,从当前城市出发,找遍周围所有可行的路径;

总结:本人认为法一比较好用

 

#include<stdio.h>
#include<string.h>//三根筋搜 
#include<stdlib.h>
#include<iostream>
using namespace std;
const int maxn=22;
int map[maxn][3],vis[maxn],path[maxn];
int c,m;
void dfs(int p,int d)
{
if(d==20&&(map[p][1]==m||map[p][2]==m||map[p][3]==m))
  {
  printf("%d:  ",++c);
   for(int i=0;i<20;i++)
    printf("%d ",path[i]);
    printf("%d\n",path[0]);
  }
 for(int i=1;i<=3;i++)
  {
  int t=map[p][i];//当前城市 
  if(!vis[t])//检查 
   {path[d]=t;//修改路径 
   vis[t]=1;
   dfs(t,d+1);
   vis[t]=0;
   }
  }
}
int main()
{
int i;
for(i=1;i<=20;i++)
 scanf("%d%d%d",&map[i][1],&map[i][2],&map[i][3]);
 while(scanf("%d",&m)!=EOF&&m!=0)
   {c=0;
   memset(path,0,sizeof(path));
   memset(vis,0,sizeof(vis));
   vis[m]=1;
   path[0]=m;
   dfs(m,1);
   }
   //system("pause");
   return 0;
}
#include<stdio.h>
#include<string.h>//一根筋搜 
#include<stdlib.h>
#include<iostream>
using namespace std;
const int maxn=22;
int map[maxn][maxn],vis[maxn],path[maxn];
int c,m;
void dfs(int p,int d)
{
if(d==20&&map[p][m])//当每个城池都走遍且最终可到达m城 
  {
  printf("%d:  ",++c);
   for(int i=0;i<20;i++)
    printf("%d ",path[i]);
    printf("%d\n",path[0]);
  }
 for(int i=1;i<=20;i++)
  {
 if(map[p][i]&&!vis[i])//当前可从p城去往i城 
     {
     path[d]=i;//修改当前路径 
     vis[i]=1;
     dfs(i,d+1);
     vis[i]=0;
     }
   }
  
}
int main()
{
int i;
int c1,c2,c3;
memset(map,0,sizeof(map));
for(i=1;i<=20;i++)
 {scanf("%d%d%d",&c1,&c2,&c3);
  map[i][c1]=map[i][c2]=map[i][c3]=1;
 }
 while(scanf("%d",&m)!=EOF&&m!=0)
   {c=0;
   memset(path,0,sizeof(path));
   memset(vis,0,sizeof(vis));
   vis[m]=1;
   path[0]=m;
   dfs(m,1);
   }
   //system("pause");
   return 0;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值