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. Python 进阶(五)定制类

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAugAAAF/CAIAAACUs6uhAAAgAElEQVR4nOzdZXubx7ov8PPV9tlrt0 ...

  2. Environment中针对的读写权限判断

    Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态,并且需要在AndroidManifes ...

  3. Redis 转

    Redis 简介 Redis实践 Redis命令总结

  4. Pascal's Triangle

    class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector< ...

  5. struts2多线程数据乱窜问题

    转自:http://love398146779.iteye.com/blog/1781680 1.struts2为每个线程提供一个action实例,多线程访问时不会出现问题.当使用spring管理st ...

  6. 在MyEclipse中搭建Spring MVC开发环境

    环境版本 IDE:MyEclipse 8.5 Spring:spring 3.2.8 JDK:1.6 1.打开MyEclipse-->File-->New-->Web Project ...

  7. listview实现点击条目上的箭头展开隐藏菜单。

    效果如下图,当点击listview中的小三角时,显示出下面布局,再点隐藏, 点击其他条目的三角时,上一个展开的条目隐藏的同时展开当前条目. 思路是在item布局中放入展开菜单的布局,并设置状态为隐藏, ...

  8. ACM题目————STL练习之Ananagrams

    Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...

  9. php之上传类

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/5/26 * Time: 20:29 */ class u ...

  10. easyui datebox 只选择年月

    //将日期输入框变为年月的函数方法    var month=0;      $('#effectiveDate').datebox({        onShowPanel: function () ...