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

HINT

回溯法解决,直接无脑暴力破解工作量巨大。结合使用深度优先遍历递归,每次递归完成之后将其恢复原状,大大节省时间。另外,由于n<=16,最大的和素数为31,直接枚举出来即可。

Accepted

#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
int n, Case = 0;
vector<int>list; int vis[17] = { 0 };
set<int>prime_number = { 2,3,5,7,11,13,17,19,23,29,31 };
void dfs(int num) {
if (num == n && prime_number.count(list.front() + list.back()))
for (int i = 0; i < n; i++)cout << list[i] << (i == n - 1 ? '\n' : ' ');
else for(int i=2;i<=n;i++)
if (!vis[i] && prime_number.count(i + list.back())) {
list.push_back(i); vis[i] = 1;
dfs(num + 1);
list.pop_back(); vis[i] = 0;
}
}
int main() {
while (cin >> n) {
cout << (Case == 0 ? "" : "\n");
memset(vis, 0, sizeof(vis));
list.clear();
cout <<"Case " << ++Case <<':'<< endl;
list.push_back(1); vis[1] = 1;
dfs(1);
}
}

Prime Ring Problem UVA - 524的更多相关文章

  1. 暴力求解——素环数 Prime Ring Problem ,UVa 524

    Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...

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

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

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

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

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

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

  6. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  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(经典DFS+回溯)

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

  9. 杭电oj 1016 Prime Ring Problem

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

随机推荐

  1. 修改vs模板抬头\Common7\IDE\ItemTemplates\CSharp\Code\2052\Class

    //************************************************************************************************** ...

  2. JS驼峰与下划线互转

    1.下划线转驼峰 function underlineToHump(s){ var a = s.split("_"); var result = a[0]; for(var i=1 ...

  3. su: Authentication failure解决方法

    su命令不能切换root,提示su: Authentication failure,需要sudo passwd root一次之后,下次再su的时候只要输入密码就可以成功登录.

  4. 阻塞队列——四组API

    方式 抛出异常 有返回值,不抛出异常 阻塞等待 超时等待 添加 add() offer() put() offer(...) 移除 remove() poll() take() poll(...) 检 ...

  5. Kubernetes-4.Pods

    docker version:19.03.14 kubernetes version:1.19.4 ** 已了解Kubernetes的组成.安装.以及kubectl基本命令使用 本文概述Kuberne ...

  6. 【python+selenium的web自动化】- 8种元素定位方式详解

    ​ 我们在做WEB自动化时,最根本的就是操作页面上的各种元素,而操作的基础便是元素的定位,只有准确地定位到唯一元素才能进行后续的自动化控制,下面将对各种元素定位方式进行总结归纳. ​ 说明:以下操作统 ...

  7. python面试题,print写在for循环内和外的区别

    1.统计列表中正数和负数的数量a = [1,3,5,7,0,-1,-9,-4,-5,8]b = []c = []for i in a : if i>0: b.append(i) elif i&l ...

  8. C# 基础 - Enum 的一些操作

    1. int 转换成 enum public enum Suit { Spades, Hearts, Clubs, Diamonds } Suit spades = (Suit)0; Suit hea ...

  9. url里bookmark是什么意思

    <a rel="bookmark" href="abc.com"> 点击查看 </a> rel 这个属性的全称是  relationsh ...

  10. 从零开始编写一个BitTorrent下载器

    从零开始编写一个BitTorrent下载器 BT协议 简介 BT协议Bit Torrent(BT)是一种通信协议,又是一种应用程序,广泛用于对等网络通信(P2P).曾经风靡一时,由于它引起了巨大的流量 ...