题目链接:

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. 【DataStructure】The description of Java Collections Framework

    The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...

  2. c语言 int (*p)[5] 类型分析

    #include<stdio.h> int main() {     int i;     int  b[5]={1,3,5,7,9};     int  (*a)[5] = &b ...

  3. ODPS 下一个map / reduce 准备

    阿里接到一个电话说练习和比赛智能二选一, 真的很伤心, 练习之前积极老龄化的权利. 要总结ODPS下一个 写map / reduce 并进行购买预测过程. 首先这里的hadoop输入输出都是表的形式, ...

  4. ios 多线程开发(三)Run Loops

    Run loops是线程相关的一些基本东西.一个run loop是一个处理消息的循环.用来处理计划任务或者收到的事件.run loop的作用是在有事做的时候保持线程繁忙,没事的时候让线程挂起. Run ...

  5. Android-管理Activity生命周期 -重新创建Activity

    按照正常的app行为,很少情况下activity会销毁,只有当用户点击了返回按钮或者activity通过调用finish()发出销毁信号.系统也有可能销毁activity如果它是停止状态并且很久没有使 ...

  6. GitLab一键式安装bitnami

    https://bitnami.com/stack/gitlab/installer https://bitnami.com/redirect/to/96764/bitnami-gitlab-8.5. ...

  7. UVa 11069 - A Graph Problem

    题目:给你一个集合{1,2,..,n},计算子集的个数,子集的元素不能相邻且不能再插入元素. 分析:dp,动态规划.相邻元素间仅仅能相差3或者2. 动态方程:f(k)= f(k-2)+ f(k-3): ...

  8. Mac OS X 在捕捉AppLAN通信包

    Mac OS X 在捕捉AppLAN通信包 一.拍摄模式 由于工作关系.经常要分析App wifi通讯协议.我的开发电脑是Mac Book. 大体有例如以下几种模式: + App与server进行ht ...

  9. iOS 中国排序

    这里分享一个中国某种方便的方法,我们放在一起的人脉资源后,方便的类别,使用自己的包, 此处所使用的方法贴,源代码可以在本文的结尾下载. 要记得加头文件 #import "NSArray+So ...

  10. W5500EVB TCP Client模式设置说明

    W5500EVB是WIZnet为了方便用户更好了解.使用W5500这款网络芯片所开发的评估板,该板採用了 STM32F103RCT6+W5500 的设计.基于 ARM 的 Cortex-M3 平台.那 ...