这个搜索............搜的我头都大了.......不过还是 懂了那么一点点...哈哈

从3/7晚上  做到3/8晚上------从女生到妇女  我都用来做着一道题了.........

所谓的 深度优先搜索   还是   递归调用自身                            关键思想是

在上面写出  满足  条件的  情况  例如 if 然后怎么怎么  然后 不满足的话 继续 调整  一点一点继续  调用尝试   如果发现   不合适的话 在调用的 后面        重新 将数据还原成   没有 尝试  时  的 样子       ,,,,,,just  so so

明天据需努力    先开始  N 皇后问题     听说 和这一道题    挺像的    .

奇数的话除了 1  其余的都是无解

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
bool isPrime(int num)
{
if (num == || num == )
{
return true;
}
if (num % != && num % != )
{
return false;
}
for (int i = ; i*i <= num; i += )
{
if (num % i == || num % (i+) == )
{
return false;
}
}
return true;
}
int n,a[],mark=,visited[];
void DFS(int m)
{
if(m==n+&&isPrime(a[m-]+)) //戒指 填充完毕 , 比且最后一个数字和第一个数字之和 也是素数
{
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
mark=;
}
for(int i=;i<=n;i++)
{
if(visited[i]==) // 这个数字 没有用过
{
if(isPrime(a[m-]+i)) // 如果上一个和这一个 之和是素数的话
{
a[m]=i;
visited[i]=;
DFS(m+);
visited[i]=;
}
}
}
}
int main()
{
int count1=;
while(scanf("%d",&n),n)
{
printf("Case %d:\n",++count1);
if(n%==&&n!=)
{
printf("No Answer\n");
continue;
}
mark=;
a[]=; //第一个位置上 一定 是 1
memset(visited,,sizeof(visited));
visited[]=;
DFS(); // 直接 开始填第二个坑 .
if(mark==)
printf("No Answer\n");
}
return ;
}

一直会有这样那样的错误 , 归结了一下还是不细心造成的 . . . . . . . . . . . . .      一点一点的改吧 .

Prime Ring Problem -- 第一个真正意义上的 搜索的更多相关文章

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

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

  2. hdu 1016 Prime Ring Problem(dfs)

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

  3. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  4. hdu 1016 Prime Ring Problem(DFS)

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

  5. 杭电oj 1016 Prime Ring Problem

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

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

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

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

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

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

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

  9. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

随机推荐

  1. 每日命令:(2)cd

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1.  ...

  2. linux初步学习有感

    经过了一段时间对linux的接触,从最开始接触到的deepin到后来我最喜欢的KaliLinux,感受到了这个我曾经并不了解的操作系统的独特魅力. 我是到了大学才知道linux这个系统的,但是在小时候 ...

  3. kata练习题

    This time no story, no theory. The examples below show you how to write function accum: Examples: ac ...

  4. PyQt5Icon图标(Icon)无法显示问题

    PyQt5中设置图标无法显示 以下源码来源PyQt5教程http://zetcode.com/gui/pyqt5/firstprograms/ import sys from PyQt5.QtWidg ...

  5. axios在实际项目中的使用介绍

    1.axios本身就封装了各种数据请求的方法 执行 GET 请求 // 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345') .then(function (r ...

  6. 多校 1010 Taotao Picks Apples(补题)

    >>点击进入原题<< 思路:题解很有意思,适合线段树进阶 考虑每次修改不叠加,因此我们可以从如何对原序列进行预处理着手.通过观察可以发现,将原序列从任意位置断开,我们可以通过分 ...

  7. Garden of Eden

    Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. 用Windows自带DOS命令提示符 制作U盘启动盘

    Windows & DOS命令提示符 & U盘 & 启动盘 用Windows自带DOS命令提示符 制作U盘启动盘.docx http://xgqfrms.blog.163.co ...

  9. Ubuntu 16.04安装WPS

    1.下载 http://community.wps.cn/download/ 根据需要对应版本进行下载deb包. 2.安装 sudo dpkg -i wps-office_10.1.0.5672~a2 ...

  10. 6、Java并发性和多线程-并发性与并行性

    以下内容转自http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html(使用谷歌翻译): 术语并发和并行性 ...