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. ReactNative 基础学习

    安卓Back键的处理·基本+高级篇 http://bbs.reactnative.cn/topic/480/%E5%AE%89%E5%8D%93back%E9%94%AE%E7%9A%84%E5%A4 ...

  2. 回顾2017系列篇(二):移动端APP设计趋势

    移动端APP在2017年经历了诸多的变化, 人工智能.聊天式的界面.响应式设计.虚拟现实(VR)和增强现实(AR)让设计师不断面临新的挑战.研究表明,用户每天耗费在手机和平板上的平均时长为158分钟, ...

  3. php-删除非空目录

    function deldir($path){ if(!is_dir($path)){ return false; } $dh = opendir($path); while(($file = rea ...

  4. Python学习(五):易忘知识点

    1.列表比较函数cmp >>> a = [1,2,3,4] >>> b = [1,2,3,4,5] >>> c = [1,2,3,4] >& ...

  5. Golang 中的坑 一

    Golang 中的坑 短变量声明  Short variable declarations 考虑如下代码: package main import ( "errors" " ...

  6. spring_boot_pay支付宝,微信,银联支付详细代码案例

    spring-boot-pay 支付服务:支付宝,微信,银联详细代码案例(除银联支付可以测试以外,支付宝和微信支付测试均需要企业认证,个人无法完成测试),项目启动前请仔细阅读 注意事项  . 友情提示 ...

  7. 初学ssm框架的信息

    ssm框架,就是Spring ,SpringMVC ,mybstis 的简称,我们是从mybstis 开始学起的,mybatis的作用作为一个连接数据库的框架,可以很好配置连接好数据库, 有mybat ...

  8. ADB 安卓开发配置环境

    下载完后将名称中含有adb的文件,和fastboot.exe复制到 c:/windows/system32目录 或c:/windows/system64目录(看自己电脑系统配置 如电脑64操作系统就写 ...

  9. Linux命令每日一个

    2014-3-31 1:39 (1)tree linux以树状的结构显示当前目录及其包含的子目录下的文件 #apt-get install tree #tree   //在当前目录下直接使用该命令即可 ...

  10. 在vim中使用zencoding/Emmet

    zencoding在vim上的插件已经改名为Emmet.vim 1. 安装,推荐使用vundle插件管理器安装,在~/.vimrc中,添加:Bundle 'Emmet.vim',输入命令vim +Bu ...