很简单的深搜 只要看出来是深搜...

注意判断最后一点是否与加一为质数

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
int n;
int ans[30];
bool vis[30];
bool ok[105];
void init()///素数打表
{
memset(ok,true,sizeof(ok));
for(int i=2;i<=100;i++)
{
for(int k=2;k*i<=100;k++)
{
ok[k*i]=false;
}
}
return ;
}
bool check(int a,int b)
{
if(ok[a+b]==true)
return true;
else return false;
}
void dfs(int w,int a)
{
vis[a]=false;
ans[w++]=a;
if(w==n&&check(a,1))
{
for(int i=0;i<w;i++)
{
printf("%d",ans[i]);
if(i==w-1)
printf("\n");
else printf(" ");
}
return ;
}
for(int i=1;i<=n;i++)
{
if(vis[i]==true)
{
if(check(i,a))
{
dfs(w,i);
vis[i]=true;
}
}
}
return ;
}
int main(){
int tt=0;
init();
while(~scanf("%d",&n))
{
tt++;
printf("Case %d:\n",tt);
memset(vis,true,sizeof(vis));
dfs(0,1);
printf("\n");
}
}

  

HDU 1016 DFS的更多相关文章

  1. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

  2. [HDU]1016 DFS入门题

    题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...

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

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

  4. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  6. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  7. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  8. DFS hdu 1016

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 #include <iostream> using namespace std; int a[ ...

  9. HDU 1016 素数环(dfs + 回溯)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 一道很典型的dfs+回溯: 根据题意首先进行初始化,即第一个位置为1,然后进行dfs, ...

随机推荐

  1. css3之currentColor

    一个css3的高效变量currentColor,能够继承(父级)当前字体的颜色属性(代表当前的标签所继承的文字颜色). 参考demo:http://www.zhangxinxu.com/study/2 ...

  2. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  3. Word2013对公式处理:样式、自动编号、交叉引用

    因快写毕业论文,专门研究了一下Word2013对公式的处理,有一点小心得,记在这里. 文章中公式展示的预期效果是:公式居中,尾端有编号,同时在文章中可以实现引用.实现该效果有很多方法,这里就说一种,其 ...

  4. HDU 4612 Warm up tarjan缩环+求最长链

    Warm up Problem Description   N planets are connected by M bidirectional channels that allow instant ...

  5. windows脚本定时执行

    linux下可以直接用cron定时任务,window下可以使用schtasks 命令代替. 第一次在win7 cmd输入: schtasks 如果出现错误:“错误:无法加载列表资源” 的问题原因很简单 ...

  6. 【HTML5】地理定位

    <!DOCTYPE html> <html> <body> <p id="demo">点击这个按钮,获得您的坐标:</p> ...

  7. 在crontab中动态写日志

    45 3 * * * setsid script -c /home/dlht/shell/coreBusiness/coreOpt.sh  >> /home/dlht/logs/coreO ...

  8. Spring的lookup-method标签

    Spring的解析源码 public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) { ...

  9. 寒假D3 A Find the Lost Sock

    Alice bought a lot of pairs of socks yesterday. But when she went home, she found that she has lost ...

  10. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...