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. 【机器学习】BP & softmax求导

    目录 一.BP原理及求导 二.softmax及求导 一.BP 1.为什么沿梯度方向是上升最快方向     根据泰勒公式对f(x)在x0处展开,得到f(x) ~ f(x0) + f'(x0)(x-x0) ...

  2. MTD下的Nand驱动

    目录 MTD下的Nand驱动 引入 平台设备资源文件 关键数据结构 平台框架 s3c24xx_nand_probe nand_scan s3c2410_nand_add_partition add_m ...

  3. openstack项目【day23】:glance基础

    本节内容 一 什么是glance 二 为何要有glance 三 glance的功能 四 glance的两个版本 五 镜像的数据存放 六 镜像的访问权限 七 镜像及任务的各种状态 八 glance包含的 ...

  4. 锁定表头和固定列(Fixed table head and columns)

    源码: /// <summary> /// 锁定表头和列 /// <para> sorex.cnblogs.com </para> /// </summary ...

  5. java反射常用类

    测试实体类 public class TestClass { public String classInfo; public String getClassInfo() { return classI ...

  6. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  7. Python3.6及以上pip安装pymssql错误的解决办法[Windows&Linux freetds安装]

    只有由于Python3.6装不上 pymssql,所以一直用Python3.5的版本. 报错界面 现在有了新的解决方法: 原帖如下: https://docs.microsoft.com/en-us/ ...

  8. 有了GPRS为什么还要LoRa和NB-IoT?【转】

    转自:https://blog.csdn.net/i_am_Banmei2/article/details/81869724 与其说是GPRS和NB-IoT的比较,不如说是传统网络与新兴网络的比较,我 ...

  9. Mysql --数据库概述1

    什么是数据(Data)? 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物, ...

  10. JAVA 数组作为方法参数—传递地址

    package Code411;//数组作为方法参数—传递地址public class DodeArrayParam { public static void main(String[] args) ...