HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html
题意:
给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右相邻的两个的和是素数。给出最后的答案。
思路:
利用回溯剪枝算法,N个数,每个数有N种状态,枚举这N个状态,枚举过程中剪枝优化。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std; const int MAXN = ;
int n;
//素数表
int isprime[MAXN] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
//判断是否重复
int used[MAXN]; //素数环
int circle[MAXN]; //判断 第 key 个位置的数字是否合法
int check(int key)
{
if( used[ circle[key] ] ) //之前被用过
return ;
if(!isprime[ circle[key] + circle[key - ] ])//和前面一个组成了非素数
return ;
if(key == n && !isprime[ circle[key] + ])//最后一个数和第一个数的和也不能是素数
return ;
return ;
} void backtrack(int key)
{
if(key > n)//回溯完毕 打印结果
{
for(int i = ; i <= n; i++)
cout <<(i == ? "" : " ") << circle[i];
cout <<endl;
}
else
{
for(int i = ; i <= n; i++) //这个位置一共有 n - 1 个状态,分别枚举
{
circle[key] = i;
if( check( key ) ) //剪枝处理
{
used[i] = ; //标记这个点已被用
backtrack(key + );
used[i] = ; //恢复现场
}
}
}
} int main()
{
int kas = ;
while(cin >> n)
{
printf("Case %d:\n", kas++);
memset(used, , sizeof(used));
memset(circle, , sizeof(circle));
circle[] = ; //第一个数字从 1 开始
backtrack(); //从第二个数字开始求解
cout << endl;
}
return ;
}
HDU1016 Prime Ring Problem (回溯 + 剪枝)的更多相关文章
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1016 Prime Ring Problem
dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
随机推荐
- APPIUM-----自动发现兼容的Chromedrivers
使用Appium Desired Capabilities:chromedriverExecutableDir chromeDriver所有版本下载路径:https://chromedriver.st ...
- jsonp、瀑布流布局、组合搜索、多级评论(评论树)、Tornado初识
1.JSONP原理剖析以及实现 1.1 同源策略限制 用django分别建立两个项目,jsonp01和jsonp02,然后再在这两个项目里分别建立一个app,比如名字叫jsonp1.jsonp2:js ...
- Python全栈 MySQL 数据库 (索引、数据导入、导出)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 表字段重命名(change) alter table 表名 ...
- 孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档
孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数据库 ...
- iptables的配置文件/etc/sysconfig/iptables不存在 linux防火墙开关命令
某linux服务器,使用 cat /etc/sysconfig/iptables命令时, 找不到文件. 1. service iptables status 使用该命令检查状态 如果之前找不到配置文件 ...
- JavaScript里面的基本函数
1.主要有三类基本函数 <script type="text/javascript"> // 普通函数 function func1(arg){ return true ...
- SPOJ 362 Ignore the Garbage 转7进制+简单大数除法
显然正着倒着看仍然是数字的只有7个数:0,1,2,5,6,8,9 所以就是用这7个数组合成不同的数. 将n转换成7进制,对应位输出即可. #include <cstdio> #includ ...
- zedboard zynq 学习 sobel 边缘检测 IP核 制作 根据 文档 Xapp890
官方文档http://www.xilinx.com/support/documentation/application_notes/xapp890-zynq-sobel-vivado-hls.pdf ...
- html页面分块加载
方法:jQuery ajax - load() 方法 这个方法可以请求html页面,并把结果放在指定元素内.
- 那些牛掰的 HTML5的API(二)
1:视频播放器 2:地理定位 我们的支持html5 的浏览器给我们提供一个接口(api),可以用来获取你当前的位置. 主要是通过geolocation(地理位置),对象 ,去访问硬件,来获取到经纬度. ...