HDU1016 DFS+回溯(保存路径)
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
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.
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.
- #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+回溯(保存路径)的更多相关文章
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 剪格子---(dfs回溯)
如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
随机推荐
- iOS SDAutoLayout图文混排-共享
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #526eda } span.s1 { color: #4dbf5 ...
- bzoj 2753: [SCOI2012]滑雪与时间胶囊
Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...
- Spring之DAO一
前面博客把bean.aop简单了解了一下,今天主要是了解Spring中DAO层,如果使用传统的JDBC时需要创建连接.打开.执行sql.关闭连接这一系列的步骤,Spring框架对JDBC进行了封装,我 ...
- Macaca环境搭建踩坑总结
1.使用命令 npm i macaca-android -g 安装一直不成功,使用Macaca doctor 一直没有显示出android C:\Users\ABC>npm i macaca- ...
- JavaScript的DOM编程--11--插入节点
插入节点: 1). insertBefore(): 把一个给定节点插入到一个给定元素节点的给定子节点的前面 var reference = element.insertBefore(newNode,t ...
- JAVA个人理解
为了找到别人写的好文章,先分享下自己的知识,找找感觉路线. 学java前接触的c,后来转向java.第一个照面理解的就是面向对象,没想到让我想了好多年.当时有个负责任的老师说面向对象这个词具体释义众说 ...
- 线程安全Dictionary
public abstract class ReadFreeCache<TKey, TValue> { protected ReadFreeCache() : this(null) { } ...
- class java.awt.HeadlessException : No X11 DISPLAY variable was set, but this program performed an operation which requires it.
今天上午打印回单功能发布到测试环境,报了: class java.awt.HeadlessException : No X11 DISPLAY variable was set, but this p ...
- 中国孩子的micro:bit:TurnipBit自制小乐器教程实例
孩子们是最贪玩的也是最聪明的,因此在过去的数年中,市面上出现了不少寓教于乐的理工科知识(STEM)学习新方法.如今这类产品中又有了一名新成员,TPYBoard重磅推出一款针对小白.中小学生的可编程计算 ...
- springboot 注册服务注册中心(zk)的两种方式
在使用springboot进行开发的过程中,我们经常需要处理这样的场景:在服务启动的时候,需要向服务注册中心(例如zk)注册服务状态,以便当服务状态改变的时候,可以故障摘除和负载均衡. 我遇到过两种注 ...