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. Django admin修改密码

    django的admin用户被我多动症一样的测试,给密码弄丢了,需要重置. 从数据库重置的可能性为0,因为django对于密码有保护策略.考虑从运行程序的地方进行重置: 1.在程序的文件夹下,执行这样 ...

  2. 第十节: EF的三种追踪实体状态变化方式(DBEntityEntry、ChangeTracker、Local)

    一. 简介 我们在前面章节介绍EF基本增删改的时候,曾说过EF的SaveChanges()方法,会一次性的将所有的实体的状态变化统一提交到数据库,那么你是否想过EF的实体会有哪些状态变化呢?什么原因会 ...

  3. 第十六节:语法总结(3)(C#6.0和C#7.0新语法)

    一. C# 6.0 新语法 1. 自动属性初始化可以赋值 /// <summary> /// 自动属性初始化 /// </summary> public class UserI ...

  4. luogu P5301 [GXOI/GZOI2019]宝牌一大堆

    传送门 wdnm又是打麻将 首先国土无双可以直接枚举哪种牌用了\(2\)次算贡献,然后\(7\)个对子可以把每种牌的对子贡献排序,取最大的\(7\)个,剩下的牌直接暴力枚举是不行的,考虑dp,设\(f ...

  5. 计算int数组中的最大,最小,平均值

    public static void testNumber(int[] arr) { int max = arr[0]; int min = arr[0]; int avg = 0; int sum ...

  6. oracle利用redo恢复

    oracle媒介恢复(Media Recovery) 官方资料 https://docs.oracle.com/database/121/ADMQS/GUID-CBC5870F-2C9A-4F67-B ...

  7. JavaWeb - Apache与Tomcat有什么关系和区别

    总结: 1- apache是web服务器,侧重于http server: tomcat是应用(java)服务器,侧重于servlet引擎 2-合作过程详解,请看:JavaWeb - apache和to ...

  8. mvc webapi+autofac + session 的使用

    先说说我的项目情况:MVC5+AUTOFAC,下面就直接说说怎么加入webapi.autofac的配置.登录使用session 一.MVC5添加WEBAPI 1.添加 参考文章:https://blo ...

  9. nslookup get public/external IP

    nslookup myip.opendns.com resolver1.opendns.com Server: resolver1.opendns.comAddress: 208.67.222.222 ...

  10. 【转载】MySQL5.7 添加用户、删除用户与授权

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...