HDU 2181 哈密顿绕行世界问题( 搜索水题bfs+dfs)
来源:互联网 发布:电脑流量查询软件 编辑:程序博客网 时间:2024/06/01 10:22
哈密顿绕行世界问题
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4129 Accepted Submission(s): 2504
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 201 3 122 4 103 5 81 4 65 7 196 8 174 7 98 10 163 9 1110 12 152 11 1312 14 2013 15 1811 14 169 15 177 16 1814 17 196 18 201 13 1950
Sample Output
1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 52: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 53: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 54: 5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 55: 5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 56: 5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 57: 5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 58: 5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 59: 5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 510: 5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 511: 5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 512: 5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 513: 5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 514: 5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 515: 5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 516: 5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 517: 5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 518: 5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 519: 5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 520: 5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 521: 5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 522: 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 523: 5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 524: 5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 525: 5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 526: 5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 527: 5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 528: 5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 529: 5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 530: 5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 531: 5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 532: 5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 533: 5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 534: 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 535: 5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 536: 5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 537: 5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 538: 5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 539: 5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 540: 5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 541: 5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 542: 5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 543: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 544: 5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 545: 5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 546: 5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 547: 5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 548: 5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 549: 5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 550: 5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 551: 5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 552: 5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 553: 5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 554: 5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 555: 5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 556: 5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 557: 5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 558: 5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 559: 5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 560: 5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5
思路:普通搜索题,水题一道,所以就分别试了试bfs和dfs。测试数据对了应该就可以直接提交,但是在输出格式上错了几次,有毒啊,注意:后面有两个空格,输出每行最后也不能有空格。。。
代码:
BFS:
#include<stdio.h>#include<queue>#include<string.h> using namespace std;int map[25][3];int n;struct Node{ int ans[25]; int cur; int len;} ;bool check(int x,Node node){int *ans=node.ans;int len=node.len;for(int i=1;i<len;i++) //5在ans[0],所以从1开始 {if(ans[i]==x) return false;}return true;}void print(int count,Node node){int *ans=node.ans;int len=node.len;printf("%d: ",count);for(int i=0;i<len;i++){ printf("%d",ans[i]); if((i+1)!=len) printf(" ");}printf("\n");}void bfs(int a){Node node;queue<Node> q; int count=0;node.cur=a;node.ans[0]=a;//把起点入队 node.len=1;q.push(node);while(!q.empty()){node = q.front();q.pop();int len=node.len;int *ans=node.ans;int cur=node.cur;//printf("cur===%d\n",cur);//if(len>20) continue;//长度大于20停止搜索 if(len==21&&ans[len-1]==a) // { print(++count,node); continue; }for(int i=0;i<3;i++){int x=map[cur-1][i];//map数组下标从0开始 if(check(x,node))//检查x是否已在队列 {node.ans[len]=x;node.cur=x;node.len=len+1;if(node.len<=21) q.push(node); }} }}int main(){ memset(map,0,sizeof(map)); for(int i=0;i<20;i++) { for(int j=0;j<3;j++) { scanf("%d",&map[i][j]); } } while(scanf("%d",&n)&&n) { bfs(n); }}DFS:
#include<stdio.h>int map[25][3];int ans[25];int n;int count;//答案个数 bool check(int x,int len,int *ans){for(int i=1;i<len;i++) //5在ans[0],所以从1开始 {if(ans[i]==x) return false;}return true;}void print(int count,int len,int *ans){printf("%d: ",count);for(int i=0;i<len;i++){ printf("%d",ans[i]); if((i+1)!=len) printf(" "); //没有if就会有格式错误。。。 }printf("\n");}void dfs(int cur,int len){//printf("%d %d\n",cur,len); // if(len>21) return ; ans[len-1]=cur; if(len==21&&cur==n)//满足条件输出 { print(++count,len,ans); } for(int i=0;i<3;i++) { int x=map[cur-1][i]; if(check(x,len,ans)) { if(len<21) dfs(x,len+1); } }}int main(){ for(int i=0;i<20;i++) { for(int j=0;j<3;j++) { scanf("%d",&map[i][j]); } } while(scanf("%d",&n)&&n) { count=0; dfs(n,1); }}
0 0
- HDU 2181 哈密顿绕行世界问题( 搜索水题bfs+dfs)
- HDU 2181 哈密顿绕行世界问题(水DFS)
- HDU 2181 哈密顿绕行世界问题 (DFS)
- HDU - 2181 哈密顿绕行世界问题(简单dfs)
- HDU 2181-哈密顿绕行世界问题(裸dfs)
- hdu 2181 哈密顿绕行世界问题(dfs)
- HDU 2181哈密顿绕行世界问题 (简单DFS)
- HDU 2181哈密顿绕行世界问题(DFS)
- 【HDU 2181】哈密顿绕行世界问题(DFS)
- HDU-2181-哈密顿绕行世界问题(DFS)
- HDU 2181哈密顿绕行世界问题 (简单DFS)
- hdu 2181 哈密顿绕行世界问题 (DFS~)
- hdu 2181 哈密顿绕行世界问题(DFS)
- HDU 2181 哈密顿绕行世界问题(搜索)
- HDU ACM 2181 哈密顿绕行世界问题->DFS(深度有限搜索)
- hdu 2182 哈密顿绕行世界问题 搜索 dfs
- HDU 哈密顿绕行世界问题(dfs)
- 【DFS】hdu 2181 哈密顿绕行世界问题
- HDU
- B1001:害死人不偿命的(3n+1)猜想 (15)
- ROS学习总结(2)--ROS框架与理念
- 小东分苹果
- mysql中间件研究( Atlas,cobar,TDDL,mycat,heisenberg,Oceanus,vitess,OneProxy )
- HDU 2181 哈密顿绕行世界问题( 搜索水题bfs+dfs)
- 《深入理解计算机系统》
- 欧拉回路
- poj2431Expedition
- poj1159 Palindrome(最长公共子序列, dp+滚动数组)
- HDU4722-Good Numbers
- Struts拦截器
- Sublime Text3 3126 Licence 注册码
- #define中#和##的作用