[HDU]1016 DFS入门题
题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列。Ps:题目是环,所以头和尾也要算哦~
典型的dfs,然后剪枝。
这题目有意思的就是用java跑回在tle的边缘,第一次提交就tle了(服务器负载的问题吧),一模一样的第二次提交就ac了,侧面也反应了递归对stack的开销影响效率也是严重的。好了,上代码!!
题目传送门:
import java.util.Scanner;
public class Main {
public static final int[] prime = { 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1 };
public static boolean[] visited;
public static int[] nums;
public static int n;
public static void dfs(int deepth,int current){
if(deepth == n ){
if(prime[current+1]==1){
for(int i=0;i<n;i++){
System.out.print( nums[i]);
if(i+1!=n){
System.out.print( " " );
}
}
System.out.println( );
}
}
else {
for(int i=2;i<=n;i++){
if(!visited[i] && prime[current+i]==1){
visited[i]=true;
nums[deepth] = i;
dfs(deepth+1,i);
visited[i]=false;
}
}
}
}
public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
int c=1;
while( sc.hasNext() ) {
n = sc.nextInt();
nums = new int[ n+1 ];
visited = new boolean[ n+1 ];
nums[0]=1;
System.out.println("Case "+c+":");
dfs(1,1);
c++;
System.out.println( );
}
}
}
[HDU]1016 DFS入门题的更多相关文章
- Oil Deposits(poj 1526 DFS入门题)
http://poj.org/problem?id=1562 ...
- Prime Ring Problem HDU - 1016 (dfs)
Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...
- HDU 1241 连通块问题(DFS入门题)
Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...
- hdu 3062 2-sat入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 #include <cstdio> #include <cmath> # ...
- HDU 1016 DFS
很简单的深搜 只要看出来是深搜... 注意判断最后一点是否与加一为质数 #include<stdio.h> #include<string.h> #include<alg ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- hdu 1455(DFS+好题+经典)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 咸鱼的ACM之路:DFS水题集
DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- OGG数据仓库以及单向复制(二)
Configure Extract(提取) Process in Source system 配置capture(捕获)参数 Edit extract process parameter G ...
- jQuery表单对象属性过滤选择器
jQuery表单对象属性过滤选择器 <div id="p1" attr="p1"> <input type="text" ...
- Bootstrap 按钮分组
Bootstrap 按钮分组: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- spring mvc handler的三种方式
springmvc.xml 三种方式不能针对一个controller同时使用 <?xml version="1.0" encoding="UTF-8"?& ...
- 在COM组件中调用JS函数
要求是很简单的,即有COM组件A在IE中运行,使用JavaScript(JS)调用A的方法longCalc(),该方法是一个耗时的操作,要求通知IE当前的进度.这就要求使用回调函数,设其名称为scri ...
- jquery 高级 学习笔记
--jquery 和 原生js可以共存,但不能混用.jquery 可以通过get()方法 转换为原生js. $("#div1").get(0).innerHTML; get() 需 ...
- 最近一年多我总结的常用mate标签-常用mate标签
昨天开始上班 ,今天晚上不是太忙 ,来写篇博客了 meta元素共有三个可选属性(http-equiv.name和scheme)和一个必选属性(content),content定义与 http-equ ...
- class path resource [config.xml] cannot be opened because it does not exist
初学Spring在用Resource rs=new ClassPathResource("applicationContext.xml");时老是遇到这个错误.后来发现用Appli ...
- 支撑Pinterest日均1000+次试验的A/B测试平台揭秘
编者按:本文详细介绍了 Pinterest 内部A/B测试平台的搭建过程,对于无论是有技术能力和资源想要自建A/B测试系统的大公司,还是想在业务中引入第三方A/B测试方法和工具的中小公司都极具参考意义 ...
- list、set、map以及array的区别
对于刚刚学习集合框架来说,如何选择list.set.map以及array是比较模糊的 在此我将对这四种情况做总结: array:数组,可以存储对象和基本数据类型,长度固定. Collection:集合 ...