Poj2531

首先把所有节点都放在一组,然后采用深度优先搜索的方法,对每一个节点都做判断是否应该移到另一组去,判断的依据是移过去和不移过去哪个得到的和值比较大(这里移去B组后的计算方法就是加上该点和在A组中的所有点的间距,和减去原本就在B组中的所有点的间距),如果移过去变小了,那么则不移过去,并剪掉该条支路。


import java.util.*;

public class Main1 {
static int ans=0;
static int map[][];
static boolean vis[]=new boolean[10010];
public static void dfs(int id,int n,int sum){
vis[id]=true;
int tem=sum;
for(int i=0;i<n;i++){
if(vis[i])
tem-=map[id][i];
else
tem+=map[id][i];
}
if(tem>ans)
ans=tem;
if(tem>sum){
for(int i=id+1;i<n;i++)
dfs(i, n, tem);
}
vis[id]=false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
map=new int [n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
map[i][j]=sc.nextInt();
dfs(0,n,0);
System.out.println(ans);
} }

poj3278

题意: FJ要抓奶牛。 开始输入N(FJ的位置)K(奶牛的位置)。
*            FJ有三种移动方法: 1、向前走一步,耗时一分钟。
*            2、向后走一步,耗时一分钟。 3、向前移动到当前位置的两倍N*2,耗时一分钟。
*            问FJ抓到奶牛的最少时间。PS:奶牛是不会动的。


import java.util.*;

public class Main1{
static int ans = 0;
static int map[][];
static boolean vis[] = new boolean[10010];
static int step[] = new int[10010];
static Queue<Integer> q = new LinkedList<Integer>(); public static int bfs(int n, int k) {
int head, next;
while (!q.isEmpty())
q.clear();
q.offer(n);
vis[n] = true;
step[n] = 0;
while (!q.isEmpty()) {
head = q.poll();
for (int i = 0; i < 3; i++) {
if (i == 0)
next = head + 1;
else if (i == 1)
next = head - 1;
else
next = 2 * head;
if (next < 0 || next > 10010)
continue;
if (!vis[next]) {
step[next] = step[head] + 1;
q.offer(next);
vis[next] = true;
}
if (next == k)
return step[k];
}
}
return 0;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
if (n >= k)
System.out.println(n - k);
else {
int ans = bfs(n, k); System.out.println(ans);
}
}
}

搜素题 --java的更多相关文章

  1. 开源搜素引擎:Lucene、Solr、Elasticsearch、Sphinx优劣势比较

    https://blog.csdn.net/belalds/article/details/82667692 开源搜索引擎分类 1.Lucene系搜索引擎,java开发,包括: Lucene Solr ...

  2. HDU 1226 超级密码 (搜素)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...

  3. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. POJ3984 BFS广搜--入门题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20816   Accepted: 12193 Descriptio ...

  8. Java实习生常规技术面试题每日十题Java基础(八)

    目录 1.解释内存中的栈(stack).堆(heap)和静态区(static area)的用法. 2.怎样将GB2312编码的字符串转换为ISO-8859-1编码的字符串? 3.运行时异常与受检异常有 ...

  9. Java实习生常规技术面试题每日十题Java基础(七)

    目录 1. Java设计模式有哪些? 2.GC是什么?为什么要有GC? 3. Java中是如何支持正则表达式. 4.比较一下Java和JavaSciprt. 5.Math.round(11.5) 等于 ...

随机推荐

  1. vegas 为盖斯

    vegas 为盖斯 S键 分割素材U键 分开视频和音频I键渲染开始O渲染结束 默认布局 为盖斯新建项目的参数 剪好后渲染 插入字幕

  2. Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档

    Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.hparm        # 它用来在基于 Linux的系统上获取或 ...

  3. 金融量化分析【day110】:Pandas-DataFrame索引和切片

    一.实验文档准备 1.安装 tushare pip install tushare 2.启动ipython C:\Users\Administrator>ipython Python 3.7.0 ...

  4. SpringBoot项目@RestController使用 redirect 重定向无效

    Spring MVC项目中页面重定向一般使用return "redirect:/other/controller/";即可. 而Spring Boot当我们使用了@RestCont ...

  5. sublime中编译的sass如何改变css输出风格?【这里有答案】

    由于在网上找了一遍没找到如果在sublime中将sass编译的css转换成为自己喜欢的风格,所以换了一种思路搜索到了答案,这里我将讲述如果更改. 首先sass总共有四种编译风格 (1) nested( ...

  6. 跨站请求伪造 | ajax

    一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功 ...

  7. Blender 2.78 突然卡顿

    之前一直好好的直到这两天打开就一直延迟半拍,重启重装无效. 解决方法: NVIDIA控制面板→管理3d设置→程序设置→选择blender,更改显卡为集成显卡→应用 亲测有效.

  8. Scrapy框架-----爬虫

    说明:文章是本人读了崔庆才的Python3---网络爬虫开发实战,做的简单整理,希望能帮助正在学习的小伙伴~~ 1. 准备工作: 安装Scrapy框架.MongoDB和PyMongo库,如果没有安装, ...

  9. Huber Loss

    Huber Loss 是一个用于回归问题的带参损失函数, 优点是能增强平方误差损失函数(MSE, mean square error)对离群点的鲁棒性. 当预测偏差小于 δ 时,它采用平方误差, 当预 ...

  10. 【原创】大叔经验分享(23)spark sql插入表时的文件个数研究

    spark sql执行insert overwrite table时,写到新表或者新分区的文件个数,有可能是200个,也有可能是任意个,为什么会有这种差别? 首先看一下spark sql执行inser ...