Prime Ring Problem

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

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
 

题意:输入一个 n 找出1~n的组合,使得相邻两个数之和为素数;
分析:预处理40之间的素数,然后回溯;
 #include<iostream>
#include<cstring>
#define N 25
#define M 40
using namespace std; bool is_prime[M],visited[N];
int n,test,ans[N]; void work(int k)
{
int i;
if(k==n+)
{
if(!is_prime[ans[n]+ans[]]) return ;
for(i=;i<=n-;i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
return ;
}
for(i=;i<=n;i++)
{
if(!visited[i]&&is_prime[ans[k-]+i])
{
visited[i]=true;
ans[k]=i;
work(k+);
visited[i]=false;
}
}
} bool prime(int n)
{
if(n==) return false;
if(n==||n==) return true;
int i;
for(i=;i<n;i++)
if(n%i==)
return false;
return true;
} int main()
{
int i;test=;
for(i=;i<M;i++) is_prime[i]=prime(i);
while(cin>>n)
{
ans[]=;
memset(visited,false,sizeof(visited));
cout<<"Case "<<test<<":"<<endl;
work();
test++;
cout<<endl;
}
return ;
}

hdu 1016 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(素数环问题)

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

  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 经典素数环

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

  9. hdu 1016 Prime Ring Problem(dfs)

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

随机推荐

  1. mac 启动nfsd共享

    # cat /etc/exports /Users/guang/jumpserver -maproot=root:wheel -alldirs -rw -network 192.168.244.0 - ...

  2. CPU informition

    tar jxvf util-linux-ng-2.18.bz2cd util-linux-ng-2.18/./configure --enable-arch --enable-partx --enab ...

  3. SQL order 排序

    select * from emp;

  4. 树形DP+二分(Information Disturbing HDU3586)

    题意:给出一颗数,1结点代表司令部,叶子节点代表前线,边全值代表花费,然后需要在某些边放置一些炸弹,炸弹的能量不能小于该边的费用,且炸掉的总费用不能超过m问炸弹能力最小多少, 分析dfs+二分,二分枚 ...

  5. linux第2天 信号 wait

    孤儿进程和僵尸进程 如果父进程先退出,子进程还没退出那么子进程的父进程将变为init进程.(注:任何一个进程都必须有父进程) 如果子进程先退出,父进程还没退出,那么子进程必须等到父进程捕获到了子进程的 ...

  6. sql语句中的 inner join 、 left join 、 right join、 full join 的区别

    简单明了地说,连接分内连接和外链接 假设有A和B两张表 内连接:inner join   表示把AB表的记录相符都显示出来,把AB表不符合条件的都排除 外连接分三种,即左连接(LEFT OUTER J ...

  7. mongo导出导入

    导出例子: mongoexport -d test -c test -q '{sn:1}' -o test.dat 导入例子: mongoimport -d test -c students stud ...

  8. 夺命雷公狗jquery---2层级选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Oracle PL/SQL中的循环处理(sql for循环)

    今天来说下Oracle中的循环迭代处理,因为从自己的博客统计中看到,不少网友都搜索了关键字"SQL FOR循环",所以打算在这里说下个人的理解. PL/SQL也和我们常用的编程语言 ...

  10. json转化为java实体

    import net.sf.json.JSONObject; Map<String, Object> classMap = new HashMap<String, Object> ...