Prime Ring Problem

HDU - 1016

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.

 

Inputn (0 < n < 20). 
OutputThe 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
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue> using namespace std; int n;
int a[];
int vis[];
bool mark[]; void init() // 素数筛
{
for(int i = ; i < ; ++i)
mark[i] = true; for(int i = ; i < ; ++i)
{
if(mark[i] == true)
{
for(int j = i*i; j < ; j += i)
{
mark[j] = false;
}
}
}
} // 边枚举边判断,不要最后一次性判断,会超时
void dfs(int step)
{
if(step > )
{
if(mark[a[step-]+a[step-]] == false) // 判断最后两个数
return;
} if(step == n+)
{
if(mark[a[n]+a[]] == false) // 判断最后一个数与第一个数
return;
for(int i = ; i < n; ++i)
printf("%d ", a[i]);
printf("%d\n", a[n]); } for(int i = ; i <= n; ++i)
{
if(!vis[i])
{
a[step] = i;
vis[i] = ;
dfs(step+);
vis[i] = ;
} }
} int main()
{
init();
int cas = ;
a[] = ;
while(scanf("%d", &n) != EOF)
{
printf("Case %d:\n", cas++);
memset(vis, , sizeof(vis));
dfs();
printf("\n");
} return ;
}

Prime Ring Problem HDU - 1016 (dfs)的更多相关文章

  1. Prime Ring Problem HDU - 1016

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  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. HDOJ(HDU).1016 Prime Ring Problem (DFS)

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

  4. HDU 1016 Prime Ring Problem (DFS)

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

  5. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  6. hdu 1016 Prime Ring Problem(dfs)

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

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

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

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

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

  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. layui实现批量导入excal表

    layui实现多文件上传,并直接选中需要上传文件的类型 //拖拽上传 upload.render({ elem : '#import', size: , //限制文件大小,单位 KB accept: ...

  2. zookeeper结构和命令

    1.1. zookeeper特性 1.Zookeeper:一个leader,多个follower组成的集群 2.全局数据一致:每个server保存一份相同的数据副本,client无论连接到哪个serv ...

  3. 廖雪峰Java15JDBC编程-3JDBC接口-2JDBC查询

    我们可以使用JDBC查询来执行select语句. 1. Statement try(Connection conn = DriverManager.getConnection(JDBC_URL, JD ...

  4. springboot指定项目访问路径前缀

    springboot默认的运行方式是没有项目前缀的 如果这时候我们要加上调试或者发布,可以使用下面的方法加上 方法:

  5. 数据交互 axios 的使用

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. ##Axios npm version build status code coverage npm ...

  6. SPOJ - UOFTCG 树的最小路径覆盖

    //SPOJ - UOFTCG 树的最小路径覆盖 #include <bits/stdc++.h> #include <vector> using namespace std; ...

  7. Python中的sort()

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可 ...

  8. leetcode 238 & leetcode 152 & leetcode 228

    lc238 Product of Array Except Self 遍历两次数组 用一个res[] 记录答案 1) 第一次,从左往右遍历 res[i] 记录0~i-1的乘积 2) 第二次,从右往左遍 ...

  9. HZOI20190814 B 不等式

    不等式 题目大意:求解满足$L \leqslant(S×x)mod M\leqslant R$的x最小正整数解,无解输出-1 几种部分分: $L==R$,就是$ex_gcd$; 解在$1e6$以内:搜 ...

  10. Django项目:CRM(客户关系管理系统)--64--54PerfectCRM实现CRM客户报名链接

    # kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...