搜素题 --java
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的更多相关文章
- 开源搜素引擎:Lucene、Solr、Elasticsearch、Sphinx优劣势比较
https://blog.csdn.net/belalds/article/details/82667692 开源搜索引擎分类 1.Lucene系搜索引擎,java开发,包括: Lucene Solr ...
- HDU 1226 超级密码 (搜素)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...
- 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 + ...
- 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 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 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 ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- Java实习生常规技术面试题每日十题Java基础(八)
目录 1.解释内存中的栈(stack).堆(heap)和静态区(static area)的用法. 2.怎样将GB2312编码的字符串转换为ISO-8859-1编码的字符串? 3.运行时异常与受检异常有 ...
- Java实习生常规技术面试题每日十题Java基础(七)
目录 1. Java设计模式有哪些? 2.GC是什么?为什么要有GC? 3. Java中是如何支持正则表达式. 4.比较一下Java和JavaSciprt. 5.Math.round(11.5) 等于 ...
随机推荐
- 一个select元素自定义设计的新思路:appearance: none之后利用<>符号制造小箭头
最近工作时解决了一个前端小问题(如下图所示):在Safari中,select的控件之上有不和谐的灰色部分. 刚开始时我以为是backgrand或是border设置不当之类产生的问题,在搜索了很久之后终 ...
- Ubuntu操作用户账户
Git Gerrit $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为了安全).想用root的话,得给root用户设置一个密码: sudo pa ...
- SpringBoot系列: Pebble模板引擎语法介绍
本文基于Pebble官方文档, 对pebble的语法做一些介绍. ===============================Pebble 官方资料========================= ...
- [译]Ocelot - Delegating Handlers
原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- v-model
仅用于以下控件: <input> <select> <textarea> 组件 v-model以Vue 实例的数据作为数据来源,应当在组件的 data 选项中声明初 ...
- ve2.0 v-for循环报错的解决方案
<li v-for="(item,index) in mokeData" class="page" :key="index"> ...
- Java并发之Thread类的使用
一.线程的几种状态 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以下这几个状态:创建(new).就绪(runnable).运行(running).阻塞(blocked).time wa ...
- Python中应该使用%还是format来格式化字符串?
转载自http://www.cnblogs.com/liwenzhou/p/8570701.html %的特点是,前面有几个%,后面的括号里就得有几个参数,如果只有一个%,括号可以省略 基本格式 'a ...
- Window10系统中MongoDB数据库导入数据文件
首先进入C:\Program Files\MongoDB\Server\4.0\bin> 打开cmd 创建一个空的数据库集合 db.createCollection("myColl ...