Prime Ring Problem dfs
Note: the number of first circle should always be 1.
OutputThe 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 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
int n,ans[]={};
using namespace std;
int vis[];
bool isnum(int x)
{
if(x<=)return false;
for(int i=;i<=sqrt(x);i++)
{
if(x%i==)return false;
}
return true;
}
void dfs(int k,int last)
{
if(k==n)
{
if(!isnum(ans[]+ans[n-]))return;
printf("%d",ans[]);
for(int i=;i<n;i++)
printf(" %d",ans[i]);
putchar('\n');
return;
}
for(int i=;i<=n;i++)
if(!vis[i]&&isnum(last+i))
{
vis[i]=;
ans[k]=i;
dfs(k+,i);
vis[i]=;
}
}
int main()
{
int k=;
while(cin>>n)
{
vis[]=;
printf("Case %d:\n",++k);
dfs(,);
putchar('\n');
}
}
Prime Ring Problem dfs的更多相关文章
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 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 ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- vs2010打包安装
[WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform 1. ...
- [.NET开发] C#编程调用Cards.dll实现图形化发牌功能示例
本文实例讲述了C#编程调用Cards.dll实现图形化发牌功能.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; usi ...
- 肠道型(enterotype)简介
An enterotype is a classification of living organisms based on its bacteriological ecosystem in the ...
- English trip -- MC(情景课)3 C Do you have a sister?
xu言: 学了困难的在去看以前的课程,发现真的容易多了.So 学习的最好方法和提速方式,那就是找困难的不断去挑战.尝试.尝试.在尝试! Grmmar ['græmə] focus ['fəʊk ...
- PHP函数总结 (五)
<?php /** * 回调函数: * 指调用函数时并不是传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中 * 使用回调函数可以 将一段自己定义的功能传到函数内部使用 * ...
- Connecting Vertices CodeForces - 888F (图论,计数)
链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l, ...
- synchronized锁普通方法和锁静态方法
1.对象锁钥匙只能有一把才能互斥,才能保证共享变量的唯一性 2.在静态方法上的锁,和 实例方法上的锁,默认不是同样的,如果同步需要制定两把锁一样. 3.关于同一个类的方法上的锁,来自于调用该方法的对象 ...
- 4.写出完整版的strcpy函数
(1) 2~4分 void strcpy(char *strDest, char *strSrc) { while((*strDest++ = *strSrc++)!='\0'); } //将源字符串 ...
- git push时候总提示输入账号密码,如何免除设置?
1. 打开.ssh所在目录 home,即C:\Users\Administrator2. 在home中,进入git bash命令终端,创建.git-credentials文件,编辑 touch .gi ...
- CUDA ---- Constant Memory
CONSTANT MEMORY constant Memory对于device来说只读但是对于host是可读可写.constant Memory和global Memory一样都位于DRAM,并且有 ...