题目链接:

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488

深搜模板:

 void dfs(int 当前状态)
{
if(当前状态为边界状态)
{
记录或输出
return;
}
for(i=;i<n;i++) //横向遍历解答树所有子节点
{
//扩展出一个子状态。
修改了全局变量
if(子状态满足约束条件)
{
dfs(子状态)
}
恢复全局变量//回溯部分
}
}

未优化的代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std; int a[]={};
bool visit[];
int n; bool isprime(int x){
int i;
for(i=;i*i<=x;i++){
if(x%i==)
return false;
}
return true;
} void DFS(int x){
int i;
if(x==n-){
if(isprime(a[x]+)){
printf("");
for(i=;i<n;i++)
printf(" %d",a[i]);
printf("\n");
return ;
}
}
for(i=;i<=n;i++){
if(visit[i]==&&isprime(a[x]+i)){
visit[i]=;
a[x+]=i;
DFS(x+);
visit[i]=;
}
}
}
int main()
{
int i;
int Case=;
while(scanf("%d",&n),n){
memset(visit,,sizeof(visit));
printf("Case %d:\n",Case++);
if(n%==||n==)
DFS();
else
printf("No Answer\n");
}
return ;
}

素数可以打表:

 #include <iostream>
#include <algorithm>
using namespace std;
bool sushu[]={,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
int a[], res[], n, flag;
void dfs(int now)
{
int i;
if (now==n&&sushu[a[n-]+a[n]])
{
flag = ;
for (i = ; i<n; i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{
for (i = ; i<=n; i++)
if (!res[i]&&sushu[i+a[now-]])
{
res[i] = ;
a[now] = i;
dfs(now+);
res[i] = ;
}
}
}
int main()
{
int N;
N=;
while (cin>>n&&n)
{
flag = ;
a[]=a[n]=;
cout<<"Case "<<N++<<":"<<endl;
if ((n-)&||n==)
dfs();
if (flag)
cout<<"No Answer\n";
}
return ;
}

NYoj 素数环(深搜入门)的更多相关文章

  1. HDU 1016(素数环 深搜)

    题意是说对一个长度为 n 的数环进行排列,使得相邻两数的和为素数,按从小到大的顺序依次输出. 因为是环,所以总能调整成以 1 为序列首输出.用深度优先搜索的方法即可.在判断素数时由于 n 小于 20, ...

  2. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

  3. NYOJ 10 skiing (深搜和动归)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...

  4. POJ 2488:A Knight's Journey 深搜入门之走马观花

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35342   Accepted: 12 ...

  5. POJ 2386 DFS深搜入门

    题目链接 Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, water has pooled in va ...

  6. NYoj 部分和问题(深搜经典)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1058 #include <stdio.h> ], vis[], co ...

  7. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

  8. 本BLOG简介(内有一道UVa524素数环进阶版)【B001】

    [B001]Hi,大家好,今天我的博客第一天开通,今天奉上开博题,出自首都师师范大学附属中学OJ(题号未知在练习场中)原题为UVa524,题目要求如下: [难度B]—————————————————— ...

  9. HDU1016 素数环---(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 Sample Input 6 8   Sample Output Case 1: 1 4 3 2 5 6 ...

随机推荐

  1. 新浪微博id的62进制转换

    某条微博链接 某条微博的链接如下,同样省略了后面的无关参数 http://weibo.com/2803301701/CeaOU15IT CeaOU15IT为这条微博的mid,与之相对应的还有一个id, ...

  2. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  3. TabHost+RadioGroup搭建基础布局

    xml的形势如下: <tabhost> <linearlayout vertival> <framlayout weight=1/> <tabwidget g ...

  4. dev XtraMessageBox按钮显示中文

    dev的XtraMessageBox控件使用起来很美观,但默认显示确定的是英文,如下图: 通过下面代码可使“OK”显示为中文:首先创建一个继承自Localizer的类: using DevExpres ...

  5. HDU 1711 Number Sequence(算法验证)

    该怎么做.每一个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在奋斗--lx_Zz ...

  6. OFTP说明

    OFTP (TheOdette File Transfer Protocol,RFC 2204)作为两个商业伙伴中建立EDI连接的一种协议.它由Odette-Organization于1986年创建. ...

  7. hibernate它5.many2one单向

    关系数据库表之间的关系: 1 正确 1 1 正确 许多 许多 正确 许多 表间关系设计 基于主键关联 基于外键关联 基于中间表 1 对 1关系实现: 基于主键关联 基于外键关联 基于中间表 1 对 多 ...

  8. hdu2852--KiKi&#39;s K-Number(段树,求第一k的数量)

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. android4.0下载问题

    近期在下载android4.0的代码,下列操作 下载Repo $ mkdir ~/bin $ PATH=~/bin:$PATH $ curl https://dl-ssl.google.com/dl/ ...

  10. Maven本地仓库配置

    一. 为什么配置? 默认情况下,maven的本地仓库在C盘下用户文件夹: .m2/repository.全部的maven构件(artifact)都被存储到该仓库中.以方便重用. 可是放在C盘一个是占用 ...