本文链接: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 (回溯 + 剪枝)的更多相关文章

  1. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  4. 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 ...

  5. hdu1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu1016 Prime Ring Problem

    dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...

  8. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

随机推荐

  1. VS的几个实用快捷键

    Ctrl + K, D格式化代码 Ctrl + L 删除一行 Ctrl + K, S调出自动代码块 svm之后二下TAB 生成Main方法 Ctrl + K,C注释代码块Ctrl+K,U取消注释

  2. 《Cracking the Coding Interview》——第14章:Java——题目4

    2014-04-26 19:02 题目:解释下C++里模板和java里泛型的区别? 解法:我很少用java,属于连语法都不过关的程度.所以这个题还真没法详细答,查了些资料以后写了以下几点. 代码: / ...

  3. Hyper-V 安装Windows 2008,08 R2,12 R2 无网卡驱动的解决办法

    最近玩 Hyper -V ,都是在网上找的资料进行操作的.后面发觉园友提供的一些操作 按部就班的做下来,别人 可以 ,我的就是不行. 最近就遇到一个很烦闷的事情.(如题) 安装好系统之后 发现 没有网 ...

  4. APPIUM-----自动发现兼容的Chromedrivers

    使用Appium Desired Capabilities:chromedriverExecutableDir chromeDriver所有版本下载路径:https://chromedriver.st ...

  5. day06_05 字典

    1.0 字典 1.1 补充知识:用id可以查找出变量的内存地址 a = 10 print(id(a)) #找出内存地址 #>>>506528496 b = 15 print(id(b ...

  6. sources.ustc.debian

    deb http://mirrors.ustc.edu.cn/debian/ jessie main contrib non-free deb-src http://mirrors.ustc.edu. ...

  7. disable-network-config

    network: {config: disabled}

  8. 平衡二叉树(AVL)

    AVL就是优化二叉查找树 平衡因子不大于1 左 < 根 < 右 具体看代码 #include<bits/stdc++.h> using namespace std; typed ...

  9. Python的HttpClient实现

    Python版本3.4(注意python的版本,python2和python3的写法不一样) 其实无非就是客户端的请求,所以python中这里使用的是urllib.request模块.主要注意的是he ...

  10. Java String.intern()_学习笔记

    参考:https://www.jianshu.com/p/0d1c003d2ff5 String.intern() String.intern()是native方法,底层调用c++中的StringTa ...