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 ...
随机推荐
- Lua面向对象 --- 多继承
工程目录结构: ParentMother.lua: ParentMother = {} function ParentMother:MortherName() print("Morther ...
- jsp post/get中接处理
jsp post/get中接处理 以参数:username为便 post接收中文比get接收中文要方便多了. <%@ page contentType="text/html;chars ...
- [Java学习] Java Object类
Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: 1 ...
- 20170716xlVba销售明细转销售单据
Sub CreateSaleList() AppSettings On Error GoTo ErrHandler Dim StartTime As Variant '开始时间 Dim UsedTim ...
- CF 711B - Chris and Magic Square
挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...
- laravel 懒加载
故事背景是什么呢? 目录大家都知道吧,一般有几个层级,根据公司需求,要将目录以树的形式展示出来,为了提高访问速度,这些目录数据要一次性读取出来的.这样的话就涉及到了查询,优化查询次数是一个很关键的事情 ...
- 32. Longest Valid Parentheses *HARD*
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Tomcat类加载器破坏双亲委派
转载:https://blog.csdn.net/qq_38182963/article/details/78660779 http://www.cnblogs.com/aspirant/p/8991 ...
- notepad++插件安装
notepad安装目录的 plugins 下重启 notepad.exe程序即可 插件下载地址 : https://sourceforge.net/projects/npp-plugins/fil ...
- 小程序animation动画效果综合应用案例(交流QQ群:604788754)
如果案例有问题,可到QQ群找到今日相关压缩文件下载测试. WXML: <view class="cebian"> <view animation="{{ ...