Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21151    Accepted Submission(s): 9465

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.
 
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

 
Source
深度搜索....无压力;;
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int str[]={,,,,,,,,,,,,,,,,,,,};
int ans[]={};
int n,cnt; /*代表搜索的深度*/
bool flag;
/*可能需要剪枝*/
void dfs(int step)
{
int i,j,temp;
if(step==n) /*说明搜索到底了!*/
{
flag=true;
temp=ans[]+ans[n-]; //开头和结尾也要判断
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
printf("%d",ans[]);
for( i=;i<n;i++)
{
printf(" %d",ans[i]);
}
puts("");
}
}
else
{
for(i=;i<n;i++)
{
if(str[i])
{
flag=true;
temp=ans[cnt-]+str[i];
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
ans[cnt++]=str[i];
str[i]=;
dfs(step+);
str[i]=ans[--cnt];
ans[cnt]=;
}
}
}
}
} int main()
{
int count=;
while(scanf("%d",&n)!=EOF)
{
cnt=;
printf("Case %d:\n",count++);
dfs();
puts("");
}
return ;
}

HDUOJ----(1016)Prime Ring Problem的更多相关文章

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

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

  2. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

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

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

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

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

  6. 杭电oj 1016 Prime Ring Problem

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

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

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

  8. HDU 1016 Prime Ring Problem (DFS)

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

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

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

  10. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

随机推荐

  1. bit byte哪些事

    1.bit 电脑中最小的计量单位,比如0或1代表一个bit,简称“位” 2.byte 电脑存储的基本单位.因为上面的bit小弟太小了,所以产生了byte.由8个bit存成了byte,最小为0最大为11 ...

  2. 3D屏保:排色榜

    3D屏保:排色榜 排色榜,是一个针对图形学中的色彩进行排序的DEMO,这里的色是色彩的意思,看成别的点进来的同学请自觉面壁.该DEMO可以按RGB,GBR,BRG,灰度值四种方式进行排序.排序算法为冒 ...

  3. fstream的使用方法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间;  在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O ...

  4. Informatica 常用组件Lookup之六 查询

    PowerCenter 基于您在查找转换中配置的端口和属性来查询查找.当第一行输入到查找转换时,PowerCenter 运行一个默认的 SQL 语句.如果使用关系查找,您可以在"查找 SQL ...

  5. 掌握Sed命令

    带书签PDF版,喜欢的话,欢迎多提意见和建议,你的支持是血蝙蝠最大的前进动力! http://download.csdn.net/detail/challenge_c_plusplus/6480007 ...

  6. iOS开发-xCode6(iOS 8)中应用程序图标和启动页面设置

    iOS8中设置应用程序图标跟之前没有什么变化,命名规则不变,不过至于设置启动页面,网上给的方式很多都是模棱两可的,东平西凑总算是把启动页面的图片设置成功了,iOS设置启动图片有两种方式一种是Launc ...

  7. Spark Streaming updateStateByKey案例实战和内幕源码解密

    本节课程主要分二个部分: 一.Spark Streaming updateStateByKey案例实战二.Spark Streaming updateStateByKey源码解密 第一部分: upda ...

  8. COM不同的线程模型对列集和同步的不同要求。

  9. 在Fedora10上安装MySQL5.0.18,告捷!

    又一次安MySQL了,为什么又呢,因为前面成功那次算配置,即配置Fedora10自带的MySQL,这个还不算自己安装的,所以有必要再安装一次,以后还装也有可能,换操作系统,换MySql版本都可能这样做 ...

  10. (LeetCode 141/142)Linked List Cycle

    1.Given a linked list, determine if it has a cycle in it. 2.Given a linked list, return the node whe ...