Prime Ring Problem

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

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
 

这道题就是回溯暴力。 首先打出一个素数表, 然后DFS回溯判断即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int is_prime(int x)
{
for(int i=; i*i<=x; i++)
if(x%i==) return ;
return ;
} int n, A[];
bool isp[], vis[];
void dfs(int cur)
{
if(cur==n&&isp[A[]+A[n-]])
{
for(int i=; i<n-; i++)
{
printf("%d ", A[i]);
}
printf("%d\n", A[n-]);
}
else for(int i=; i<=n; i++)
if(!vis[i]&&isp[i+A[cur-]])
{
A[cur] = i;
vis[i] = ;
dfs(cur+);
vis[i]=;
}
} int main()
{
int kase = ;
while(scanf("%d", &n)!=EOF)
{
printf("Case %d:\n", ++kase);
for(int i=; i<=n*; i++)
isp[i] = is_prime(i);
memset(vis, , sizeof(vis));
A[] = ;
dfs();
printf("\n");
}
return ;
}
 

HDU1016 Prime Ring Problem(DFS回溯)的更多相关文章

  1. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  2. hdu1016 Prime Ring Problem(DFS)

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

  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. hdu1016 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. Prime Ring Problem(dfs水)

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

  7. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  8. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  9. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

随机推荐

  1. webpack笔记_(3)_First_Project

    知道了怎么样安装,那么学习一下简单的应用吧. 1.安装webpack npm install webpack -g (全局) npm install webpack --save--dev (本地) ...

  2. Sublime Text 3 常用插件以及安装方法

    安装Sublime Text 3插件的方法: 一.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Packages目录(菜单->preferences->p ...

  3. SPOOL、SQLLOADER数据导出导入的一点小总结

    1.SQLLOADER的CONTROL文件 //**************************************************************************** ...

  4. Could not write metadata for '/taiping-sol-insu-composite'.

    Could not write metadata for '/taiping-sol-insu-composite'. 这是由于删除一个项目时,没有同时在硬盘上删除该项目,而后又到硬盘文件系统中删除了 ...

  5. ffmpeg无法接收组播流问题处理

    问题:ffmpeg无法对IP组播进行处理,表现如下 [root@os01 /]# ffprobe udp://225.0.0.2:9000 ffprobe version Copyright (c) ...

  6. mina

    http://bsr1983.iteye.com/blog/1886296 http://blog.csdn.net/defonds/article/category/1844073(这个网站原创最全 ...

  7. C#:数据交互

    JS与Web交互:http://www.docin.com/p-76710976.html 一.WinForm的WebBrowser控件与JS交互数据: 1.C#类内的代码执行JS脚本函数: 给C#类 ...

  8. $q服务的API详解

    下面我们通过讲解$q的API让你更多的了解promise异步编程模式.$q是做为angularjs的一个服务而存在的,只是对promise异步编程模式的一个简化实现版,源码中剔除注释实现代码也就二百多 ...

  9. MultiSelectComboBox(一)

    1. MultiSelectComboBox.xaml <UserControl x:Class="MultiSelectComboBox.MultiSelectComboBox&qu ...

  10. shell脚本判断文件类型

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...