[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 ...
随机推荐
- ThinkPHP创建应用的一般开发流程
使用ThinkPHP创建应用的一般开发流程是: 系统设计.创建数据库和数据表:(可选) 项目命名并创建项目入口文件,开启调试模式: 完成项目配置: 创建项目函数库:(可选) 开发项目需要的扩展(模式. ...
- 处理div 在IE6 IE7 IE8 下不居中的问题
具体处理方式如下:1 .html 顶部加入:DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- SVN官方版本下载地址
TortoiseSVN 客户端 & Language packs 语言包 : https://tortoisesvn.net/downloads.html VisualSVN 插件官方地址: ...
- SLS评测报告
什么是SLS? 简单日志服务(Simple Log Service,简称SLS)是针对日志收集.存储.查询和分析的服务.用户只需简单地配置日志产生的位置和格式等信息,就能实时查询海量日志,并可通过S ...
- C++中的Traits技法
Traits广泛应用于标准程序库.Traits classes使得"类型相关信息"在编译期可用. 认真读完下面的示例,你应该就懂了Traits技法,其实并不难. #include ...
- loadrunner:web services接口测试
本文以实例讲解web services接口测试操作,内容包括:脚本生成.参数化和接口与接口间的取值关联操作. 网站"http://www.webxml.com.cn/zh_cn/web_se ...
- [Scoi2010]游戏
游戏 Time Limit:5000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- 照片提取GPS 转成百度地图坐标
感谢: 小慧only http://www.cnblogs.com/zhaohuionly/p/3142623.html GPS转化坐标方法 大胡子青松 http://www.cnblogs.com ...
- page cache 与free
我们经常用free查看服务器的内存使用情况,而free中的输出却有些让人困惑,如下: 先看看各个数字的意义以及如何计算得到: free命令输出的第二行(Mem):这行分别显示了物理内存的总量(tota ...
- ubuntu 笔记一
注:ubuntu14.04 64位 1.刚安装的ubuntu无法在终端使用su 原因:root没有默认密码,需要手动设定. 解决方法:以具有sudo权限的用户登录 给root用户设置密码:打开一个te ...