解题思路:哈密顿环游世界问题。一道简单的题目,用回溯。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
int vis[maxn], mapp[maxn][maxn], path[maxn];
int m, x, kase = ; void Print()
{
printf("%d: ", kase ++); //两个空格
for(int i = ; i <= ; i++)
{
printf("%d ", path[i]);
}
printf("%d\n", m);
return ;
} //now为当前城市,num为已有的城市个数
void DFS(int now, int num)
{
path[num] = now;
vis[now] = ; //now标记为已经访问过
if(num == ) //表示已经有20个城市
{
//如果第20个城市与m相连,则打印出来
if(mapp[now][m]) Print();
}
else
{
for(int i = ; i <= ; i++)
{
//如果这个城市没有访问过,并且与now相连,继续搜
if(!vis[i] && mapp[now][i]) DFS(i, num+);
}
}
vis[now] = ; //回溯
return ;
} int main()
{
memset(vis, , sizeof(vis));
memset(mapp, , sizeof(mapp));
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
scanf("%d", &x);
mapp[i][x] = mapp[x][i] = ;
}
}
while(~scanf("%d", &m) && m)
{
DFS(m, );
}
return ;
}

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

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

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

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

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

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

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

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

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

  5. 【HDU - 2181】哈密顿绕行世界问题(dfs+回溯)

    -->哈密顿绕行世界问题 Descriptions: 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市.  Input 前2 ...

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

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

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

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

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

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

  9. 哈密顿绕行世界问题(dfs+记录路径)

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

随机推荐

  1. 【Semantic segmentation】Fully Convolutional Networks for Semantic Segmentation 论文解析

    目录 0. 论文链接 1. 概述 2. Adapting classifiers for dense prediction 3. upsampling 3.1 Shift-and-stitch 3.2 ...

  2. 解题报告:poj1061 青蛙的约会 - 扩展欧几里得算法

    青蛙的约会 writer:pprp Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 119716 Accepted: 25238 ...

  3. [javascript]jquery选择器笔记

    技术文档 中文:http://jquery.cuishifeng.cn/ 英文:http://api.jquery.com/category/selectors/ 分类 基本选择器 层次选择器 过滤选 ...

  4. Java 类的构造器的调用顺序

    规则如下: 对于一个复杂的对象,构建器的调用遵照下面的顺序: (1) 调用父类构建器.这个步骤会不断重复下去,首先得到构建的是分级结构的根部,然后是下一个子类,等等.直到抵达最深一层的子类. (2) ...

  5. panda 函数-处理空值

    今天这里谈的函数,以后进行数据分析的时候会经常用到. import numpy as npimport pandas as pdfrom pandas import DataFrame , Serie ...

  6. spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

    spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...

  7. (3) iOS开发之UI处理-UIView篇

    在UIView作为许多子控件的容器的时候,首先我们需要动态的计算出UIView下的所有子控件的高度,并布局排列好,然后我们还要把作为容器的UIView的高度调整到刚好包裹着所有子控件,不会过矮,也不会 ...

  8. IOS-网络(网页开发-UIWebView,HTML,CSS,JavaScript,OC和JS代码互调)

    一.网页基础 // // ViewController.m // IOS_0218_网页开发1 // // Created by ma c on 16/2/18. // Copyright © 201 ...

  9. Ansible 小手册系列 十六(Playbook Debug)

    debug模块在执行期间打印语句,并且可用于调试变量或表达式,而不必停止playbook. 打印自定义的信息 - debug: msg="System {{ inventory_hostna ...

  10. 跨域问题Java方式解决及Nginx方式解决【亲测可行】

    这两天和前端同事调试微信公众号项目,就遇到了跨域问题:网上相关博客也挺多的,但有很多细节没有点到,在此呢我也再次记录一下解决方式: (算是踩坑日记吧~ ~ ~)   !问题发现: 页面加载不出来,控制 ...