传送门

Description

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 ≤ 16)

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. You are to write a program that completes above process.

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,2,3,……,n组成一个环,使得相邻两个整数之和均为素数。输出时序列头为1开始的序列,同一个环应恰好输出一次

题解:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 40;
bool is_prime[maxn],vis[maxn];
int n,a[maxn];

void dfs(int cur)
{
	if (cur == n && is_prime[a[0] + a[n-1]])  //递归边界,因为是环,所以还要测试第一个和最后一个
	{
		printf("%d",a[0]);
		for (int i = 1;i < n;i++)	printf(" %d",a[i]);
		printf("\n");
	}
	else
	{
		for (int i = 2;i <= n;i++)    //尝试放置每个数i
		{
			if (!vis[i] && is_prime[i + a[cur-1]])  //如果i没有用过,并且与前一个数之和为素数
			{
				a[cur] = i;
				vis[i] = true;                     //设置使用标志
				dfs(cur+1);
				vis[i] = false;                    //清楚标志
			}
		}
	}
}

int main()
{
	//提前预处理素数表
	memset(is_prime,true,sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;
	for (int i = 2;i < maxn;i++)
	{
		if (is_prime[i])
		{
			for (int j = 2 * i;j < maxn;j += i)
			{
				is_prime[j] = false;
			}
		}
	}
	int tcase = 0;
	while (~scanf("%d",&n))
	{
		memset(vis,false,sizeof(vis));
		memset(a,0,sizeof(a));
		if (tcase)	printf("\n");
		printf("Case %d:\n",++tcase);
		a[0] = 1;
		dfs(1);
	}
	return 0;
}

  

  

UVa 524 Prime Ring Problem(回溯法)的更多相关文章

  1. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  2. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

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

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

  4. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  5. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  6. UVa 524 Prime Ring Problem【回溯】

    题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...

  7. UVa 524 - Prime Ring Problem

    题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...

  8. uva 524(Prime Ring Problem UVA - 524 )

    dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...

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

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

随机推荐

  1. SAP CRM 使用Javascript触发SAP Server Event

    原文地址:How To Trigger SAP Server Event With Javascript 本文地址:http://www.cnblogs.com/hhelibeb/p/5977921. ...

  2. Scala 包

    包的绝对地址_root_.开始 如_root_.scala.collection.mutable.ArrayBuffer

  3. An App ID with Identifier 'com.XXX.XXX’ is not available. Please enter a different string.报错

    昨天刚刚升的Xcode7.3和iOS9.3,然后没怎么使用这两样就下班了,但是今天早上来了之后,就发现突然之间不能真机测试和运行代码了,一看才发现xcode报错: An App ID with Ide ...

  4. 初识Message Queue之--基础篇

    之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...

  5. 如何利用mount命令将另外一个linux服务器上的目录挂在到本机?

    你先要在192.168.1.100上开启NFS服务并编辑/etc/exports文件: chkconfig --level 35 nfs on service nfs start vi /etc/ex ...

  6. 0036 Java学习笔记-多线程-创建线程的三种方式

    创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...

  7. python 发送邮件

    # coding=utf-8 import smtplibfrom time import sleepfrom email.mime.text import MIMETextfrom email.mi ...

  8. Surface在C++层的创建源码解析

    Surface在C++层的创建源码解析 源码为:android4.4.4 1.创建SurfaceComposerClient绘图客户端 // create a client to surfacefli ...

  9. JSP动作元素——————实践篇

    本篇在理论的基础上实现不同JSP页面间的跳转 使用 Eclipse Java EE IDE 创建一个新的 Java Web 项目,具体步骤如下: (1)启动 Eclipse Java EE IDE,在 ...

  10. Android的Handler机制

    Handler机制的原理 Android 的 Handler 机制(也有人叫消息机制)目的是为了跨线程通信,也就是多线程通信.之所以需 要跨线程通信是因为在 Android 中主线程通常只负责 UI ...