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. my.cnf 配置文件参数解释

    my.cnf 配置文件参数解释: #*** client options 相关选项 ***# #以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如果你 ...

  2. Intel® Core™ i5-5300U Processor

    3M Cache, up to 2.90 GHz Specifications Ordering and Compliance Essentials     Product Collection 5t ...

  3. 【BZOJ】【2750】【HAOI2012】Road

    最短路+拓扑序DP orz zyf & lyd 统计每条边在多少条最短路径上……其实可以统计 有多少条最短路径经过了x,以及y出发到达任意一个结束点有多少种走法(沿最短路) 我们可以用Dijk ...

  4. unity 3D游戏场景转换

    //////////////////2015/07/07//////// /////////////////by xbw/////////////// ///////////////环境 unity ...

  5. 使用SGD(Stochastic Gradient Descent)进行大规模机器学习

    原贴地址:http://fuliang.iteye.com/blog/1482002  其它参考资料:http://en.wikipedia.org/wiki/Stochastic_gradient_ ...

  6. 国内各视频网站android pad客户端支持分辨率情况初步统计

    视频网站名称 800*600 1024*600 1280*800 其他 国际化   备注 优酷 支持 支持 支持 支持 不支持     土豆网 没有pad版的 没有pad版的 没有pad版的 支持 不 ...

  7. C++初始化列表和大括号中构造的差别

    C++的对象构造函数有两种初始化的方法: 1.初始化列表 2.大括号中面赋值 这两种推荐使用另外一种.原因在于使用初始化列表仅仅须要进行一次初始化.而使用大括号内赋值的话首先须要调用默认构造函数初始化 ...

  8. 项目加入 TFS报错

      新建一个项目,然后在解决方案上右击,选择Add solution to source control的时候,总是失败,output窗口中出现的错误信息如下: An error was raised ...

  9. [Backbone] Parse not formatted JSON code

    The good Dr. recently had another team implement the server and they slightly messed up the format o ...

  10. UML建模学习1:UML统一建模语言简单介绍

    一什么是UML? Unified Modeling Language(UML又称为统一建模语言或标准建模语言)是国际对象管理组织OMG制定的一个通 用的.可视化建模语言标准.能够用来描写叙述(spec ...