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. SpringBoot 项目初始化

    工作之余,想要学习一下SpringBoot,通过网络大量教程最终成功运行SpringBoot项目.  第一步 首先,通过教程发现一套完整的快速搭建SpringBoot项目网站:https://star ...

  2. linux系统解压命令总结

    原文链接:https://www.cnblogs.com/lhm166/articles/6604852.html tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追 ...

  3. nodejs+express+mongodb实现登录注册

    nodejs+express+mongodb实现登录注册 1 简介 登录注册功能使用nodejs+express+mongodb完成,其中对mongodb的操作使用mongoose完成,对mongod ...

  4. Wayland architecture

    Introduction Motivation Most Linux and Unix-based systems rely on the X Window System (or simply X) ...

  5. 关于Java中for,while,if,方法的练习

    练习 计算0到100之间的奇数和偶数和 package com.kangkang.forDemo;​public class demo01 {    public static void main(S ...

  6. JUC-ThreadLocalRandom

    目录 Radndom类的局限性 ThreadLocalRandom 这个类是在JDK7中新增的随机数生成器,它弥补了Random类在多线程下的缺陷. Radndom类的局限性 在JDK7之前包括现在j ...

  7. JQGrid 应用

    jqGrid 原理 jqGrid是典型的B/S架构,服务器端只是提供数据管理,客户端只提供数据显示.换句话说,jqGrid可以以一种更加简单的方式来展现你数据库的信息,而且也可以把客户端数据传回给服务 ...

  8. PicGo+Typora+Gitee设置图床

    PicGo图床 使用 Typora 编辑 MarkDown 非常方便,但是图片插入后只能保存在本地,十分讨厌 所以,可以使用图床技术,将图片先保存到网络端,再应用到 Typora 中 PicGo应用获 ...

  9. Reactive Spring实战 -- WebFlux使用教程

    WebFlux是Spring 5提供的响应式Web应用框架. 它是完全非阻塞的,可以在Netty,Undertow和Servlet 3.1+等非阻塞服务器上运行. 本文主要介绍WebFlux的使用. ...

  10. vscode配置golang开发环境手把手描述篇

    1.下载安装Golang https://golang.google.cn/dl/ 一路下一步即可 2.下载安装Vscode https://visualstudio.microsoft.com/zh ...