Problem Description
A ring is
compose of n circles as shown in diagram. Put natural number 1, 2,
..., n into each circle separately, and the sum of numbers in two
adjacent circles should be a prime.



Note: the number of first circle should always be 1.



Ring Problem" title="Prime Ring Problem">
Input
n (0 < n
< 20).
Output
The output
format is shown as sample below. Each row represents a series of
circle numbers in the ring beginning from 1 clockwisely and
anticlockwisely. The order of numbers must satisfy the above
requirements. Print solutions in lexicographical order.



You are to write a program that completes above process.



Print a blank line after each case.
Sample Input
6
8
Sample Output
Case
1:
1 4 3 2 5
6
1 6 5 2 3
4
Case
2:
1 2 3 8 5 6
7 4
1 2 5 8 3 4
7 6
1 4 7 6 5 8
3 2
1 6 7 4 3 8
5 2
题意:给你一个n,得到一个数组,1~n;让你写出一个素数环,要求相邻两个数(顺逆时针)和是素数,输出的时候第一个永远是一;
解题思路:按照递归求全排列的思想,从第2个开始递归一直到最后一位,并且首尾也是素数才算是搜索完成;
感悟:一开始我还以为得剪纸,但是只有20个数,一次就过了;
代码:
#include

#include

#include

#include

#define maxn 25

using namespace std;

int ans[maxn],visit[maxn],n;

int Prime(int a)

{

    for(int
i=2;i<=sqrt(a);i++)

       
if(a%i==0)

           
return 0;

    return
1;

}



void dfs(int cur)

{

   
if(cur==n&&Prime(ans[0]+ans[n-1]))//递归到最后一位,并且首尾也能是素数

    {

       
for(int i=0;i

           
printf("%d ",ans[i]);

       
printf("%d\n",ans[n-1]);

    }

    else

    {

       
for(int i=2;i<=n;i++)

       
{

           
if(!visit[i]&&Prime(i+ans[cur-1]))//这个数没用过并且相邻的是素数

           
{

               
ans[cur]=i;

               
visit[i]=1;

               
dfs(cur+1);

               
visit[i]=0;

           
}

       
}

    }

}

int main()

{

   
//freopen("in.txt", "r", stdin);

    int
s=1;

   
memset(visit,0,sizeof(visit));

   
while(~scanf("%d",&n)&&n)

    {

       
for(int i=0;i

           
ans[i]=i+1;

       
printf("Case %d:\n",s++);

       
dfs(1);

       
printf("\n");

    }

}

Prime Ring Problem的更多相关文章

  1. uva 524 prime ring problem——yhx

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

  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. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  4. 杭电oj 1016 Prime Ring Problem

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

  5. hdu 1016 Prime Ring Problem(深度优先搜索)

    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. HDU 1016 Prime Ring Problem (DFS)

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

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

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

  9. HDU 1016 Prime Ring Problem (回溯法)

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

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

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

随机推荐

  1. 第4章 同步控制 Synchronization ----互斥器(Mutexes)

    Win32 的 Mutex 用途和 critical section 非常类似,但是它牺牲速度以增加弹性.或许你已经猜到了,mutex 是 MUTual EXclusion 的缩写.一个时间内只能够有 ...

  2. TCHAR

    定义 TCHAR :通过define 定义的字符串宏 因为C++支持两种字符串:常规的ANSI编码 (使用""包裹).Unicode编码(使用L" "包裹).因 ...

  3. Javascript 中 ==(相等运算符) 和 ===(严格相等运算符) 区别

    在JS中,"==="叫做严格运算符,"=="叫做相等运算符. 它们的区别是相等运算符(==)比较两个值是否相等,严格相等运算符(===)比较它们是否为" ...

  4. DAO与DTO

    DAO叫数据访问对象(data access object) DTO是数据传输对象(data transfer object) DAO通常是将非对象数据(如关系数据库中的数据)以对象的方式操纵.(即一 ...

  5. python之字典

    1.用{}创建字典 代码: 1 2 x = {"a":"1", "b":"2"} print x 输出: {'a': ' ...

  6. IDL 存储数组

    IDL中的数组在内存中是按行存储的,这是因为IDL最初设计的设计目的是用来处理行扫描卫星数据. 1.一维数组 m个元素的一维数组arr[m]的存储方式为 arr[0]→arr[1]→...→arr[m ...

  7. html加载时事件触发顺序

    一般情况下页面的响应加载顺序时,域名解析-加载html-加载js和css-加载图片等其他信息. jq ready()的方法就是Dom Ready,他的作用或者意义就是:在DOM加载完成后就可以可以对D ...

  8. JS判断浏览器类型与版本

    在JS中判断浏览器的类型,估计是每个编辑过页面的开发人员都遇到过的问题.在众多的浏览器产品中,IE.Firefox.Opera.Safari........众多品牌却标准不一,因此时常需要根据不同的浏 ...

  9. JavaScript实现模糊推荐的input框(类似百度搜索框)

    如何用JS实现一个类似百度搜索框的输入框呢,再填充完失去焦点时,自动填充配置项,最终效果如下图: 实现很简单,但是易用性会上升一大截,需要用到的有jquery-ui的autocomplete,jque ...

  10. ASP.NET MVC5+EF6+EasyUI 后台管理系统(87)-MVC Excel导入和导出

    本文示例代码下载: 链接:http://pan.baidu.com/s/1jHBdgCA 密码:hzh7 ps:Vs数据库脚本在解压目录下,修改web.config数据库链接,示例代码包含:导入,导出 ...