本文链接: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. 《Cracking the Coding Interview》——第8章:面向对象设计——题目8

    2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...

  2. 网页图片很多时,加载完后再加载图片(defer:延迟加载)

    图片影响页面加载速度,可以先加载完页面,再去加载图片. defer:告诉浏览器,这里面的js代码不影响网页脚本解析,可以解析完html脚本再执行这段js代码(个人理解). 网页代码:<img s ...

  3. Pythonyield使用浅析

    转自:https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python ...

  4. selenium初识(二)——之webdriver API

    配置完的环境之后,我们先来写一个小脚本: # __Author__:"Jim_xie" from selenium import webdriver from time impor ...

  5. [类和对象]3 C++面向对象模型初探

    ? C++编译器如何完成面向对象理论到计算机程序的转化? [C++编译器是如何管理类.对象.类和对象之间的关系] 通过下面的代码,我们可以的得出:C++类对象中的成员变量和成员函数是分开存储的 成员变 ...

  6. .NET Core Linux 部署实践

    部署环境:CentOS 7 x64 必要条件:当前用户有sudo权限 1. 安装依赖包sudo yum install libunwind libicu2. 下载.net core安装文件curl - ...

  7. 解决使用vim-go插件时候保存go代码导致设置好的折叠消失的问题

    我之前在用vim编辑python代码的时候,折叠的功能都没啥问题 后来在编辑go代码的时候,我发现我一保存,折叠全都消失了,我很费解,就推断跟我使用的插件有关系,因为我保存的时候会触发gofmt插件格 ...

  8. J2EE的十三个技术——EJB之消息驱动JMS

    JMS--Java Message Service JAVA的消息服务,消息可实现两端通信. 用于访问面向消息中间件的标准api,他提供与厂商无关的访问方法,以访问消息收发服务. 特点:即使其中一方不 ...

  9. composer 库无法提交git

    composer require firebase/php-jwt 安装了该库,想提交到版本库,无论如何也提交不上去,.ignore中也没忽略,网上寻找答案,说有个.git文件夹需要删除 PHPSTO ...

  10. select下拉框右对齐,去掉箭头,替换箭头

    右对齐 select{ width:auto; direction: rtl; } select option { direction: ltr; } 去掉箭头(不设置背景色会有灰色背景) selec ...