Prime Ring Problem

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


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的每个数构成一个环,相邻两个数之和为素数,将符合的结果按字典序全部输出;
方法采用的是DFS,将每个数都遍历一边,找出符合的情况;


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int a[25];//保存结果
int flag[25];//记录一个数是否已用过
bool isprime(int n)
{
if (n == 1 || n == 0)
return 0;
if (n == 2)
return 1;
for (int i = 2; i <= sqrt((double)n); i++)
{
if (n%i == 0)
return 0;
}
return 1;
}
void dfs(int tot)
{
if (tot == n-1)
{
if (isprime(a[n - 1] + a[0]))
{ int o = 0;
for (int i = 0; i < n; i++)
{
while (o++)
{
printf(" ");
break;
}
printf("%d", a[i]);
}
printf("\n");
}
return;
}
for (int i = 2; i <= n; i++)
{
if (flag[i] == 0)
{
if (isprime(a[tot] + i))
{
flag[i] = 1;
a[tot + 1] = i;
dfs(tot + 1);
flag[i] = 0;
}
}
}
}
int main()
{
int k = 1;
while (~scanf("%d", &n))
{
printf("Case %d:\n", k++); a[0] = 1;
memset(flag, 0, sizeof(flag));
dfs(0);
printf("\n");
}
return 0; }

Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏的更多相关文章

  1. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  2. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  3. 多校赛3- Solve this interesting problem 分类: 比赛 2015-07-29 21:01 8人阅读 评论(0) 收藏

    H - Solve this interesting problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I ...

  4. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  5. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  7. Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  9. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. Java 和 Javascript的关系

    写这篇文章是因为看到有人问这个问题,在想怎么会有这种SB问题,不过想想当初SB的我貌似也搞不清两者的关系,认知还是需要一个过程. 然后看到比较经典的回答有:Java 和Javascript的关系就像雷 ...

  2. redis 数据类型为list命令整理以及示例

    常用来制作队列,当然lpush+rpop也能做栈 #将RPUSH RPUSHX LPUSH LPUSHX一并介绍(具体介绍RPUSH和RPUSHX,因为其实就是插入的方向的区别) RPUSH key ...

  3. Real-time qPCR So Easy?

    Real-time qPCR So Easy? [2016-05-27]       实时荧光定量PCR技术是在定性RCR技术基础上发展起来的核酸定量技术,于1996年由美国Applied biosy ...

  4. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  5. Servlet会话管理二(Cookie)

    Cookie是在HTTP协议下,将服务器传递给浏览器的的少量信息保存到浏览器客户端的一种技术,通过这种技术,即使在浏览器被关闭或链接中断的情况下,用户仍可以维护Cookie中的数据. Cookie是经 ...

  6. Eloquent Attach/Detach/Sync Fires Any Event

    eloquent-attach-detach-sync-fires-any-event I have a laravel project, and I need to make some calcul ...

  7. Autofs

    1. Introduction autofs is a program for automatically mounting directories on an as-needed basis. Au ...

  8. 买茶叶想到的哪个比较便宜 x1/y1 >x2/y2 x代表多少钱 y代表 多少克 无聊的试炼

    茶叶1 128元     200克 茶叶2  330元    160克 当然这个哪个便宜 一眼就知道了,这里不过抛砖引玉 128元    330元 200克    160克 我们把价钱用x表示 多少克 ...

  9. Python3实战系列之一(获取印度售后数据项目)

    问题:公司在印度开设生产工厂并在当地销售手机,生产.销售系统均由印度开发维护.对总部需要的售后数据,采用每日在ftp上提供一个.xlsx文件,给总部使用.总部需要将此数据导入到总部的销量统计系统中,以 ...

  10. 重新设置Linux的IP地址(该操作会永久更改ip地址)

    1.查看你当前的IP地址 2.进入配置文件进行更改IP地址 3.上图我使用的是ifcfg-eth1 ,然后进行更改这个文件 4.点击“insert”进行编辑改文档,吧对应的IP改成你想要的地址 更改完 ...