DFS:Prime Ring Problem(素数环)
解体心得:
1、一个回溯法,可以参考八皇后问题。
2、题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序。不用去特意排序。
3、输出有个坑,就是在输出一串的最后不能有空格,不然要PE,很尴尬。
题目:
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 3 8 5 2
Source
Asia 1993 8 5 2
Source
Asia 19 Shanghai (Mainland China)
#include<stdio.h>
#include<cstring>
int n,num[21],sum,l,sum1;
bool prim[50];
bool bnum[21];
int circle[21];
int z = 1;
void dfs(int sum)
{
if(sum == n)
{
if(!prim[circle[1]+circle[sum]])//判断第一个和最后一个相加是否是素数
return;
for(int j=1;j<=n;j++)
{
if(j!=n)
printf("%d ",circle[j]);//最后一个数字后面不可以加空格,不然要PE,很尴尬;
else
printf("%d",circle[j]);
}
printf("\n");
return;
}
for(int i=2;i<=n;i++)
{
if(!bnum[i])
continue;
if(prim[i+circle[sum]])
{
circle[sum+1] = i;
bnum[i] = false;
dfs(sum + 1);
bnum[i] = true;
}
}
return;
}
int main()
{
//先把50以内的素数手写出来
memset(prim,false,sizeof(prim));
prim[2] = true;
prim[3] = true;
prim[5] = true;
prim[7] = true;
prim[11] = true;
prim[13] = true;
prim[17] = true;
prim[19] = true;
prim[23] = true;
prim[29] = true;
prim[31] = true;
prim[37] = true;
prim[41] = true;
prim[43] = true;
prim[47] = true;
while(scanf("%d",&n)!=EOF)
{
//特判几个很大但是没有任何结果的数,直接输出,节省时间
if(n == 19 || n == 15 || n == 11 || n == 17 || n == 13)
{
printf("Case %d:\n",z++);
printf("\n");
continue;
}
printf("Case %d:\n",z++);
sum1 = 0;
l = 1;
sum = 1;
memset(circle,0,sizeof(circle));
memset(bnum,true,sizeof(bnum));
bnum[1] = false;
circle[1]=1;
dfs(1);
printf("\n");
}
}
DFS:Prime Ring Problem(素数环)的更多相关文章
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- Prime is problem - 素数环问题
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- 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 ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- smarty基本用法
简介: 1.smarty语法:它是php的一种模板引擎 它的设计特点是:业务逻辑与显示逻辑分离 Smarty的标签都是使用定界符{ }括起来注释:{* 我是Smarty的注释内容 *} <u ...
- 快速获取雪碧图的图标样式插件 - gulp-css-spriter教程
如何快速把合成好的雪碧图,快速获取图标的样式呢? 用gulp-css-spriter很简单. 第一步: 在某个文件夹用shitf+鼠标右键 第二步: npm install gulp-css-spri ...
- Eucalyptus-NC管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- git图形管理工具
在windows下使用git命令行工具对非开发人员还是挺困难的,还好有TortoiseGit这个工具svn客户端用TortoiseSVNgit客户端用TortoiseGit 网址:https://to ...
- COGS 750. 栅格网络流
★★☆ 输入文件:flowa.in 输出文件:flowa.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决 ...
- 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案
更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...
- linux网卡的配置(解决刚刚安装linux,Xshell连接不上问题)
1.输入用户名和密码 2.cd到网卡文件夹 3.对网卡文件进行编辑 vi ifcfg-eth0 然后 a 进行编辑 然后esc退出,shift+zz保存 4.重启网卡 /etc/init.d/netw ...
- 【洛谷1993】小K的农场(差分约束系统模板题)
点此看题面 大致题意: 给你若干组不等式,请你判断它们是否有解. 差分约束系统 看到若干组不等式,应该很容易想到差分约束系统吧. \(A-B≥C\):转换可得\(A-B≥C\) \(A-B≤C\):转 ...
- python序列化(数据本地存放持久性存储)和反序列化
http://blog.csdn.net/uestcyao/article/details/7874817 #读取图片并存储为矩阵 from scipy.misc import imread im = ...
- solr dataimport
solrconfig.xml <requestHandler name="/dataimport" class="org.apache.solr.handler.d ...