Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55053    Accepted Submission(s):
24366

Problem Description
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.

 
Input
n (0 < n < 20).
 
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.
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
 
题意   给你一个n  对1 ... n   n个数排序 1必须在第一位 两个数相加要为素数(1与最后一个数相加也必须为素数) 输出所有满足上述的序列。
解析 我们先1~n分别求出与它相加为素数的数存进数组,然后就相当于建了一个图 从1出发 一层一层向下推 基础DFS 然后在回溯一下 记录路径就好了
AC代码
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int maxn= ;
const int maxm= 1e4+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int visit[maxn],order[maxn];
vector<int> v[maxn];
int n,cnt;
int isPrime(int num )
{
int tmp =sqrt( num);
for(int i= ;i <=tmp; i++)
if(num %i== )
return ;
return ;
}
void dfs(int x)
{
visit[x]=; //标记访问过
order[cnt++]=x; //记录路径
for(int i=;i<v[x].size();i++) //df搜边
{
if(visit[v[x][i]]==)
{
dfs(v[x][i]);
}
}
visit[x]=; //回溯
if(cnt==n&&isPrime(x+)) //长度为n且最后一个数与1相加为素数
{
for(int i=;i<cnt;i++)
{
if(i==cnt-)
printf("%d\n",order[i]);
else
printf("%d ",order[i]);
}
}
cnt--;
}
int main()
{
int kase=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
v[i].clear();
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(isPrime(i+j)==&&i!=j) //保存能够与i相加为素数的数
v[i].push_back(j);
}
}
// for(int i=1;i<=n;i++)
// {
// cout<<i;
// for(int j=0;j<v[i].size();j++)
// printf(" %d",v[i][j]);
// cout<<endl;
// }
memset(visit,,sizeof(visit)); //初始化访问数组 0未访问过
printf("Case %d:\n",kase++);
cnt=; //路径长度初始化
dfs();
printf("\n");
}
}

HDU1016 DFS+回溯(保存路径)的更多相关文章

  1. HDU1016 Prime Ring Problem(DFS回溯)

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

  2. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  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 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. C++ 知识点总结复习

    C++ 1.C++是静态类型语言,使用静态类型的编程语言是在编译时执行类型检查,而不是在运行时执行类型检查. 2.面向对象程序设计 C++ 完全支持面向对象的程序设计,包括面向对象开发的四大特性: 封 ...

  2. MySQL数据库 Event 定时执行任务.

    一.背景 由于项目的业务是不断往前跑的,所以难免数据库的表的量会越来越庞大,不断的挤占硬盘空间.即使再大的空间也支撑不起业务的增长,所以定期删除不必要的数据是很有必要的.在我们项目中由于不清理数据,一 ...

  3. Java 读者写者问题

    实验存档.V 允许好几个人同时读,但是不允许在有人读的时候写,以及同一时间只能有一个人在写. 读者.java: package operating.entity.readerwriter; impor ...

  4. 在海航云中部署 keepalived

    **本文属自我学习,不适合转载** 1. 准备工作 1.1 网络方面的准备工作 1.1.1 创建安全组 注意这里要添加 vrrp 协议支持,否则 keepalived 将无法正常工作. 1.1.2 创 ...

  5. lesson - 5 Linux用户和组管理

    1. /etc/passwd由 : 分隔成7个字段(1) 用户名 规则:大小写字母.数字.减号(不能出现在首位).点以及下划线,其他字符不合法 (2) x 放密码,安全起见放到 /etc/shadow ...

  6. vue常见错误及解决办法

    1.在配置路由并引入组件后,报错: Unknown custom element: <router-link> - did you register the component corre ...

  7. Kafka的基本概念与安装指南(单机+集群同步)

    最近在搞spark streaming,很自然的前端对接的就是kafka.不过在kafka的使用中还是遇到一些问题,比如mirrormaker莫名其妙的丢失数据[原因稍后再说],消费数据offset错 ...

  8. arm-linux-objdump反汇编使用指南

    一.   arm-linux-objdump常用来显示二进制文件信息,常用来查看反汇编代码 二.   常用选项: 1.-b bfdname 指定目标码格式 2.-disassemble或者-d 反汇编 ...

  9. 使用 BeanUtils 报错解决记录

    在使用BeanUtils.populate方法时,报错如下: java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHa ...

  10. java 类方法和实例方法 以及 类变量和实例变量

    类体中的方法分为实例方法和类方法两种,用static修饰的是类方法 类方法: 对于类中的类方法,在该类被加载到内存时,就分配了相应的入口地址.从而类方法不仅可以被类创建的任何对象调用执行,也可以直接通 ...