搜素题 --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) 等于 ...
随机推荐
- 一条sql执行过长的时间,你如何优化,从哪些方面?
1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化) 2.涉及链表的查询,是否能进行分表查询,单表查询之后的结果进行字段整合 3. ...
- js重点--闭包
闭包: 1.获取到局部变量,相当于是函数局部与外部的桥梁 2.使局部变量保存在内存中,不被回收 <script> function outerFn() { var outerVar = 0 ...
- Unity3D 热更新方案总结
如何评价腾讯在Unity下的xLua(开源)热更方案? Unity 游戏用XLua的HotFix实现热更原理揭秘 腾讯开源手游热更新方案,Unity3D下的Lua编程 [Unity]基于IL代码注入的 ...
- 爬虫工程师JD归纳
核心能力归纳 负责:多平台信息的抓取,清洗和分析工作 要求: 熟悉常用开源爬虫框架,如 scrapy / pyspider 了解基于Cookie的登录原理,熟悉常用的信息抽取技术,如正则表达式.XP ...
- 边缘检测之Canny
1. 写在前面 最近在做边缘检测方面的一些工作,在网络上也找了很多有用的资料,感谢那些积极分享知识的先辈们,自己在理解Canny边缘检测算法的过程中也走了一些弯路,在编程实现的过程中,也遇到了一个让我 ...
- Tomcat zabbix监控、jmx监控、zabbix_java_gateway
几种方式监控tomcat,如标题. 下面就是参考的网上的连接.自己可以试一下. 由于牵扯到jvm的很多东西, 在这里就只是粘贴处连接参考. http://www.cnblogs.com/chrisDu ...
- C#基础之Assembly
一直以来,我们都在用C#编写程序,编写程序的时候,我们用到继承.多态.接口以及泛型,我们也都明白子类可以继承抽象类,并能够重写父类的抽象方法,可是大家是否想过,如下几个问题: 1.凡树必有根和叶,类的 ...
- 如何开启远程debug调试功能?
远程debug步骤: 1.vi /usr/local/sa/tomcat-ui/bin/catalina.sh 最顶上加export JPDA_ADDRESS=12345 2.vi /usr/loca ...
- Alpha 冲刺 (9/10)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之9 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩准备中 和大佬们跟进进度 接下来的计划 准备答辩 ...
- 关闭VS警告 warning C4996
warning C4996: '_vsnprintf': This function or variable may be unsafe. ...... warning C4996: strcpy w ...