题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列。Ps:题目是环,所以头和尾也要算哦~

典型的dfs,然后剪枝。

这题目有意思的就是用java跑回在tle的边缘,第一次提交就tle了(服务器负载的问题吧),一模一样的第二次提交就ac了,侧面也反应了递归对stack的开销影响效率也是严重的。好了,上代码!!

题目传送门:

HDU_1016

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入门题的更多相关文章

  1. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  2. 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 ...

  3. HDU 1241 连通块问题(DFS入门题)

    Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...

  4. hdu 3062 2-sat入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 #include <cstdio> #include <cmath> # ...

  5. HDU 1016 DFS

    很简单的深搜 只要看出来是深搜... 注意判断最后一点是否与加一为质数 #include<stdio.h> #include<string.h> #include<alg ...

  6. 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, ...

  7. hdu 1455(DFS+好题+经典)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

  9. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. BNU Online Judge-34973-Liserious战队

    题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=34973 题目不难,很容易,不过不仔细看题可能你一直做不出,注意<<一共分为1~ ...

  2. ThinkPHP--IS_AJAX

    增加IS_GET,IS_POST,IS_PUT,IS_DELETE,IS_AJAX常量,方便除控制器外的地方判断方法,Action类的isGet isPost等方法暂时保留,但不建议使用.

  3. XMLSocket的bug

    "<cross-domain-policy>" "<site-control permitted-cross-domain-policies=\&quo ...

  4. spring mvc redirect设置FlashAttribute

    在Controller中设置: @RequestMapping("/redir") public String redir(Model model, RedirectAttribu ...

  5. pureMVC介绍及学习

    1   简介 Pure MVC是在基于模型.视图和控制器MVC模式建立的一个轻量级的应用框架,这种开源框架是免费的,它最初是执行的ActionScript 3语言使用的Adobe Flex.Flash ...

  6. 深入理解DOM事件类型系列第六篇——加载事件

    前面的话 提到加载事件,可能想到了window.onload,但实际上,加载事件是一大类事件,本文将详细介绍加载事件 load load事件是最常用的一个事件,当页面完全加载后(包括所有图像.java ...

  7. Ansible_自动化运维《Ansible之初识-1》

    1.Ansible简介 1.1 Ansible介绍 Ansible 是一个简单的自动化运维管理工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...

  8. SQL语句的优化建议

    重中之重---语句执行顺序   我们先看看语句的执行顺序 如果我没记错这是<SQL SERVER 2005技术内幕--查询>这本书的开篇第一章第一节.书的作者也要让读者首先了解语句是怎么样 ...

  9. Java面向对象知识点

    对象:一切客观存在的事物都是对象 语法部分: 类的概念:1.类是对象的抽象 2.类是客观事物在人脑中的主观反应 3.类是对象的模板 类的设计: 属性:定义位置:类以内,方法以外 实例变量:1 有默认值 ...

  10. JavaScript中国象棋程序(7) - 置换表

    "JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第2节. 这一系列共有9个部分: 0.JavaScript中国象 ...