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. 鸿蒙js开发7 鸿蒙分组列表和弹出menu菜单

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口]目录:1.鸿蒙视图效果2.js业务数据和事件3.页面视图代码4.跳转页面后的视图层5.js业务逻辑部分6.<鸿蒙js开发& ...

  2. linux 安装node和pm2

    用yum安装 curl -sL https://rpm.nodesource.com/setup_10.x | bash - yum install -y nodejs npm install -g ...

  3. E百科 | 基于MEC的边缘AI服务

    简介: 阿里云边缘计算团队付哲解读5G下热门场景:边缘AI.作者:阿里云付哲,计算机科学与技术专业博士后,在流量检测.资源调度领域有深入研究,其论文<Astraea: Deploy AI Ser ...

  4. 深入剖析 ConcurrentHashMap

    自建博客地址:https://bytelife.net,欢迎访问! 本文为博客自动同步文章,为了更好的阅读体验,建议您移步至我的博客 本文作者: Jeffrey 本文链接: https://bytel ...

  5. Fastdfs数据迁移方案

    1.     方案背景描述 环境迁移,需要迁移旧环境的fastdfs集群的数据到新环境,由于之前数据迁移仅仅是针对mysql和mongodb,对fastdfs数据的迁移了解甚少,本文档主要是针对fas ...

  6. 分布式实时处理系统——C++高性能编程

    [前言]基于通信基础,介绍Hurricane实时处理系统的工程实现,主要使用C++语言. 一.IPC.socket.异步I/O epoll 二.C++11 1.linux内存管理中使用RALL原则,C ...

  7. Dos常用命令整理

    Dos常用命令整理 打开cmd的方法 开始菜单 -> 系统 -> 命令提示符 组合键Win+R打开运行 -> 输入cmd 在任意文件夹下Shift+鼠标右键 -> 在此处打开命 ...

  8. double型数据的输入和输出--%f和%lf

    scanf函数是通过指针指向变量的. %f告诉scanf函数在所传地址位置上存储一个float型值, 而%lf告诉scanf函数在所传地址位置上存储一个double型值. 这里float和double ...

  9. ORM框架 和 面向对象编程

    ORM框架: 1.SQLAlchemy:  - 作用   1.提供简单的规则   2.自动转换成SQL语句  - DB first/code first   DB first: 手动创建数据库以及表  ...

  10. 推荐!!! Markdown图标索引网站

    作者:三十三重天 博客: http://www.zhouhuibo.club 我们在观察别人的文章时候时,总能看到很多有趣的图标,像是这样