Prime Ring Problem

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

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

题意:在1到n构成一个圆环两两相邻的数和为素数,输出所有情况。
思路:感觉搜索最重要的就是找状态,这里的状态就是(当前这个数,已经放置了的个数)。
收获:回溯法:因为有可能再次使用一个数,所以回溯。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500 int n;
int vis[maxn];
int a[maxn];
int judge(int x)
{
if(x <= ) return ;
int m = floor(sqrt(x) + 0.5);
for(int i = ; i <= m; i++)
if(x%i == ) return ;
return ;
}
void dfs(int pos, int num)
{
a[num] = pos;
if(num == n && judge(pos+))
{
for(int j = ; j <= n-; j++)
printf("%d ", a[j]);
printf("%d\n", a[n]);
return;
}
for(int i = ; i <= n; i++)
{
int sum = pos + i;
if(judge(sum) && !vis[i])
{
vis[i] = ;
dfs(i, num+);
vis[i] = ;
}
}
}
int main()
{
int cas = ;
while(~scanf("%d", &n))
{
flag = ;
memset(vis, , sizeof vis);
printf("Case %d:\n", cas++);
dfs(, );
puts("");
}
return ;
}

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

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

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

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

  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. hdu 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 (DFS)

    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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

随机推荐

  1. VC断点不可用的问题

    文章一 今天我笔记本的VC编译器莫名其妙的就进入不了断点了,而台式机确是好的,问题是一进入调试,断点就自动被取消了,郁闷了一下午,无法进入断点的原因很多,都有想重装VC编译器的打算了,晚上回来查查资料 ...

  2. Validate Binary Search Tree 解答

    Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...

  3. poj 2395 Out of Hay(最小生成树,水)

    Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...

  4. execute immediate的简单用法(oracle)

    直接上示例代码: create or replace procedure proc_test( --参数区域 ) is --变量区域 --sql脚本 v_sql ) :=''; --记录学生数量 v_ ...

  5. Linux 下的 Nginx 反向代理配置.

    最近实践中遇到了需要利用 nginx 进行反向代理服务器请求的需求,以前没怎么碰触过,因此花了1个多小时,快速阅览了一下nginx官网在反向代理服务中给出的基本定义: 说实话,官网给予的定义是精准的, ...

  6. hdu2095 像水题的不错题 异或运算

    异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...

  7. 关于.netFramework概述

    这几天学了不少东西,想来还是应该总结一下,一来自己回顾一下,二来也怕自己忘记,在文章中,有很多东西就是借鉴别人的blog,笔者在这里对他们的无私奉献表示感谢.笔者的语言组织能力有限,如果在文章中出现什 ...

  8. 实现接口IDisposable的示例

    想使用using(...), 如: using (Getter process = new Getter()) { //... } 就必须给类实现接口IDisposable public sealed ...

  9. jquery创建动态的div

    参考:http://blog.csdn.net/ge_zhiqiang/article/details/6958230

  10. 结构体 + typedef

    简单结构体 struct student{ char name[20];   //可以用scanf或者直接赋值 *如果用char *name  在用scanf时没有内存接收 long id; int ...