Prime Ring Problem

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

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

 
 
 
 
简单DFS,给新生讲课用,题目要求顺时针和逆时针输出,实际好像不需要考虑这一点,最后的情况刚好是这样,素数判断用平方根内有无能整除此数的数字来判断。
 #include <iostream>
#include <cmath>
using namespace std; int N;
int ANS[];
bool VIS[]; bool isprimer(int n);
void dfs(int len);
int main(void)
{
int count = ; while(cin >> N)
{
count ++;
fill(VIS,VIS + ,false);
cout << "Case " << count << ":" << endl;
dfs();
cout << endl;
} return ;
} void dfs(int len)
{
if(len == N && isprimer(ANS[len - ] + ANS[]))
{
for(int i = ;i < N - ;i ++)
cout << ANS[i] << ' ';
cout << ANS[N - ];
cout << endl;
return ;
} for(int i = ;i <= N;i ++)
{
if(!len && i > )
return;
if(len && !VIS[i])
{
if(isprimer(i + ANS[len - ]))
{
ANS[len] = i;
VIS[i] = true;
dfs(len + );
VIS[i] = false;
}
}
else if(!len)
{
ANS[len] = i;
VIS[i] = true;
dfs(len + );
VIS[i] = false;
}
} } bool isprimer(int n)
{
for(int i = ;i <= sqrt(n);i ++)
if(n % i == )
return false;
return true;
}

HDU 1016 Prime Ring Problem (DFS)的更多相关文章

  1. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  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. HDU 1016 Prime Ring Problem(素数环问题)

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

  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. hdu 1016 Prime Ring Problem(dfs)

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

  7. hdu 1016 Prime Ring Problem(DFS)

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

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

    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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

随机推荐

  1. SpringMVC 避免IE执行AJAX时,返回JSON出现下载文件

    <?xml version="1.0" encoding="UTF-8"?> <!-- SpringMVC配置文件 --> <be ...

  2. Struts2类型转换器

    概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串.    –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...

  3. delphi debug release区别是什么?

    1. 基础知识介绍: Debug编译:是为了便于程序调试,所以目标代码里附加有许多额外的东西.Release编译:是产品可作为正式拷贝发布了,已经不需要那些仅为调试而编译进去东西. (在 Releas ...

  4. STC89c52RC 的EEPROM和AVR的EEPROM

    二者的EEPROM不是一回事,AVR片内的EEPROM是独立于程序存储器的数据存储器,本身不能存储程序并运行,但现代MCU很多支持IAP,利用IAP技术可在程序存储空间实现数据存储即替代EEPROM, ...

  5. [iOS微博项目 - 1.4] - 各种item NavigationBar & NavigationItem & BarButtonItem || TabBar & TabBarItem

    一.UINavigationItem1> 获得方式self.navigationItem // self是指控制器2> 作用可以用来设置当前控制器顶部导航栏的内容// 设置导航栏中间的内容 ...

  6. Tomcat参数配置

    一.调整JVM参数 JAVA_OPTS= -server -Xms256m -Xmx1024m 注: -server: 启用服务器模式 一定要作为第一个参数,如果CPU多,服务器机建议使用此项 -Xm ...

  7. MES生产日报存储过程

    USE [ScreenMonitor]GO/****** Object: StoredProcedure [dbo].[ImportProductForDay] Script Date: 04/11/ ...

  8. Squid代理服务器&&搭建透明代理网关服务器

    案例需求 ——公司选用RHEL5服务器作为网关,为了有效节省网络带宽.提高局域网访问Internet的速度,需要在网关服务器上搭建代理服务,并结合防火墙策略实现透明代理,以减少客户端的重复设置工作 需 ...

  9. Hadoop on Mac with IntelliJ IDEA - 1 解决input path does not exist问题

    本文讲述使用IntelliJ IDEA时遇到Hadoop提示input path does not exist(输入路径不存在)的解决过程. 环境:Mac OS X 10.9.5, IntelliJ ...

  10. 图片攻击-BMP图片中注入恶意JS代码 <转载>

    昨天看到一篇文章<hacking throung images>,里面介绍了如何在BMP格式的图片里注入JS代码,使得BMP图片既可以正常显示, 也可以运行其中的JS代码,觉得相当有趣. ...