Prime Ring Problem

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

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

 
Source
深度搜索....无压力;;
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int str[]={,,,,,,,,,,,,,,,,,,,};
int ans[]={};
int n,cnt; /*代表搜索的深度*/
bool flag;
/*可能需要剪枝*/
void dfs(int step)
{
int i,j,temp;
if(step==n) /*说明搜索到底了!*/
{
flag=true;
temp=ans[]+ans[n-]; //开头和结尾也要判断
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
printf("%d",ans[]);
for( i=;i<n;i++)
{
printf(" %d",ans[i]);
}
puts("");
}
}
else
{
for(i=;i<n;i++)
{
if(str[i])
{
flag=true;
temp=ans[cnt-]+str[i];
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
ans[cnt++]=str[i];
str[i]=;
dfs(step+);
str[i]=ans[--cnt];
ans[cnt]=;
}
}
}
}
} int main()
{
int count=;
while(scanf("%d",&n)!=EOF)
{
cnt=;
printf("Case %d:\n",count++);
dfs();
puts("");
}
return ;
}

HDUOJ----(1016)Prime Ring Problem的更多相关文章

  1. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  2. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  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. hdu 1016 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. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. key-value 多线程server的Linux C++实现

    项目需求 整体思路 网络通信 字符解析 数据存储与查询 1 存储管理 2 数据查询 多线程 待改进未实现的想法 GitHub源代码 项目需求 设计一个基于Socket或基于HTTP的server,服务 ...

  2. Android布局优化之ViewStub、include、merge使用与源码分析

    在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...

  3. scala的一些特殊用法

    1.创建多行字符串,只要把多行字符串放在3个双引号间("""...""")即可.这是Scala对于here document,或者叫here ...

  4. Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)

    // DivTest.h // 异常的处理 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. ...

  5. Informatica 常用组件Lookup之九 配置未连接的查找转换

    在映射中,未连接的查找转换与管道是分开的.您可以使用 :LKP 引用限定符编写表达式以调用其它转换中的查找.未连接查找的常用用法包括: 测试表达式中某个查找的结果 基于查找结果过滤行 基于查找的结果将 ...

  6. 第六章 HashSet源码解析

    6.1.对于HashSet需要掌握以下几点 HashSet的创建:HashSet() 往HashSet中添加单个对象:即add(E)方法 删除HashSet中的对象:即remove(Object ke ...

  7. Jump Game II leetcode java

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  8. MediaPlayer 状态机 API 详解 示例

    简介 public class android.media.MediaPlayer extends Object implements VolumeAutomation 可能需要的权限: One ma ...

  9. dp 密度 分辨率 屏幕 状态栏 标题栏 适配

    一篇总结的非常完善的博文:http://www.jianshu.com/p/ec5a1a30694b 屏幕像素参数相关信息表格 屏幕级别 像素密度 每英寸像素数   通常分辨率 分辨率别称    默认 ...

  10. Oracle中的数值处理方法

    求绝对值函数 ) from dual; 求平方根函数 ) from dual; 求幂函数 ,3) from dual; 求余弦三角函数 select cos(3.14159) from dual; 求 ...