Prime Ring Problem

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

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
 

题意:输入一个 n 找出1~n的组合,使得相邻两个数之和为素数;
分析:预处理40之间的素数,然后回溯;
 #include<iostream>
#include<cstring>
#define N 25
#define M 40
using namespace std; bool is_prime[M],visited[N];
int n,test,ans[N]; void work(int k)
{
int i;
if(k==n+)
{
if(!is_prime[ans[n]+ans[]]) return ;
for(i=;i<=n-;i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
return ;
}
for(i=;i<=n;i++)
{
if(!visited[i]&&is_prime[ans[k-]+i])
{
visited[i]=true;
ans[k]=i;
work(k+);
visited[i]=false;
}
}
} bool prime(int n)
{
if(n==) return false;
if(n==||n==) return true;
int i;
for(i=;i<n;i++)
if(n%i==)
return false;
return true;
} int main()
{
int i;test=;
for(i=;i<M;i++) is_prime[i]=prime(i);
while(cin>>n)
{
ans[]=;
memset(visited,false,sizeof(visited));
cout<<"Case "<<test<<":"<<endl;
work();
test++;
cout<<endl;
}
return ;
}

hdu 1016 Prime Ring Problem(深度优先搜索)的更多相关文章

  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 (回溯法)

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

  9. hdu 1016 Prime Ring Problem(dfs)

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

随机推荐

  1. python ftplib,smtplib,poplib学习

    一.ftplib from ftplib import FTP ftpobj = FTP(IP或域名)     #实例化对象 ftpobj.login('username','passwd')  ## ...

  2. C# WebApi传参之Get请求-AJAX

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    在介绍本篇博客之前,先来温故下AJax的请求, ...

  3. fackbook的Fresco的Image Pipeline以及自身的缓存机制

    fackbook的Fresco的Image Pipeline以及自身的缓存机制 配置之前.首先需要知道两点:一点是Bitmap缓存.一点是如果你仅仅需要一个缓存,那么不调用setSmallImageD ...

  4. paper 45:latex的使用

    本教程面向对LaTeX完全无认知无基础的新人. 旨在让新人能够用最简单快捷的方式,轻松入门,能够迅速使用LaTeX完成基本的文本编辑. 尤其旨在破除部分新人对LaTeX在传闻中难以学习的恐惧感. 在入 ...

  5. php基础知识和函数

    <?php /* echo "hello","aaaa"; //输出语法,可以输出多个字符串 print "world"; //可以输 ...

  6. 在linux中搭建git服务器

    个人觉得, 以下搭建git服务器的过程就像是在linux增加了一个用户, 而这个用户的登录shell是 git-shell, 太刨根问底的东西我也说不清楚, 还是看下面的过程吧. 过程参考了网上的文章 ...

  7. 【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数

    [<zw版·Halcon与delphi系列原创教程>Halcon图层与常用绘图函数 Halcon的绘图函数,与传统编程vb.c.delphi语言完全不同,     传统编程语言,甚至cad ...

  8. zw版【转发·台湾nvp系列Delphi例程】CheckDifference1

    zw版[转发·台湾nvp系列Delphi例程]CheckDifference1 procedure TForm1.Button1Click(Sender: TObject);var img, img1 ...

  9. memcached +php环境配置和分析

    一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...

  10. WM_SIZE

    procedure WMSize (var Message: TWMSize); message WM_SIZE; 参数说明 wParam: Specifies the type of resizin ...