Prime Ring Problem

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

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

题目链接:HDU 1016

经典的DFS回溯题目,以前一直想做来着,但是不懂回溯搜索,现在类似的一些题还是挺简单的……,这题用输出外挂可以优化到200+MS,题意是把1-n中所有自然数全部排完才能算一个环,刚开始搞错了输出爆炸……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=50;
int prime[N];
int pos[N],vis[N];
int n;
void Out(int a)
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
inline bool check()
{
for (int i=1; i<=n; ++i)
{
if(!vis[i])
return false;
}
return true;
}
void dfs(int now)
{
if(now==n)
{
if(prime[pos[now]+pos[1]]&&check())
{
for (int i=1; i<=n; ++i)
{
Out(pos[i]);
putchar(i==n?'\n':' ');
}
}
return ;
}
for (int i=2; i<=n; ++i)
{
if(!vis[i]&&prime[i+pos[now]]&&now<=n)
{
vis[i]=1;
pos[now+1]=i;
dfs(now+1);
pos[now+1]=0;
vis[i]=0;
}
}
}
int main(void)
{
int i,j;
for (i=0; i<N; ++i)
prime[i]=1;
prime[1]=0;
for (i=2; i<N; ++i)
for (j=2; j*i<N; ++j)
prime[i*j]=0;
int tcase=0,m;
while (~scanf("%d",&n))
{
MM(pos,0);
vis[1]=1;
pos[1]=1;
printf("Case %d:\n",++tcase);
dfs(1);
putchar('\n');
}
return 0;
}

HDU 1016 Prime Ring Problem(经典DFS+回溯)的更多相关文章

  1. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  2. hdu 1016 Prime Ring Problem(dfs)

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

  3. hdu 1016 Prime Ring Problem (dfs)

    一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...

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

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

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

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

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

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

  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 (DFS)

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

随机推荐

  1. 自定义Notification

    private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...

  2. 被忽视但很实用的那部分SQL

    一.前言 虽然我们大多数人都学习过SQL,但是经常忽略它.总是会自以为学到的已经足够用了,从而导致我们在实际开发的过程中遇到复杂的问题后只能在检索数据后通过传统的代码来完成,但是其中很多的功能利用SQ ...

  3. windows脚本定时执行

    linux下可以直接用cron定时任务,window下可以使用schtasks 命令代替. 第一次在win7 cmd输入: schtasks 如果出现错误:“错误:无法加载列表资源” 的问题原因很简单 ...

  4. 很多k线形态或k线组合是需要验证的

    1.十字星:表明多空力量平衡.是否翻转,需要验证. 2.要反应市场心理的k线才是有效的K线,不然就是伪k或伪k线形态.

  5. 框架模式 MVC 在Android中的使用

    算来学习Android开发已有2年的历史了,在这2年的学习当中,基本掌握了Android的基础知识.越到后面的学习越感觉困难,一来是自认为android没啥可学的了(自认为的,其实还有很多知识科学), ...

  6. Hark的数据结构与算法练习之鸡尾酒排序

    算法说明 鸡尾酒排序又叫定向冒泡排序,鸡尾酒搅拌排序,搅拌排序,涟漪排序,回来排序,快乐小时排序. 鸡尾酒排序是交换排序的一种,它是冒泡排序的一个轻微的变种.冒泡是从低向高比较排序,鸡尾酒从低向高,从 ...

  7. diff和common

    diff 命令 diff命令:找出两个文件的不同点,用于比较文件的差异 linux上非常重要的工具,一般用于制作补丁文件,特别是比较两个版本不同的文件以找到改动的地方. diff在命令行中打印每一个行 ...

  8. Xamarin Android开发实战(上册)大学霸内部资料

    Xamarin Android开发实战(上册)大学霸内部资料   试读文档下载地址:http://pan.baidu.com/s/1jGEHhhO 密码:vcfm 介绍: 本教程是国内唯一的Xamar ...

  9. mysql之对触发器的操作

    1. 为什么要使用触发器? 触发器与函数有些类似,都需要声明,执行.但是触发器的执行不是由程序调用,也不是由手工启动,而是由事件来触发,激活,从而实现执行. 当触发DELETE,INSERT,UPDA ...

  10. Week,Month, Year 日期区间辅助类

    我们在做一些业务系统的时候,经常会用到一些获取时间段的情况.比如要统计某一周.某月.某年 这样一些时间区间内的一些业务数据.这时候我们就需要获取当前时间段内的一些起止日期.这里分享一个通用的日期辅助类 ...