POJ3278_Catch That Cow(JAVA语言)
思路:bfs裸题。三个选择:向左一个单位,向右一个单位,向右到2*x
//注意,需要特判n是否大于k,大于k时只能向左,输出n-k。第一次提交没注意,结果RE了,,
Catch That Cow
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 137789 | Accepted: 42551 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
Source
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Point{
public int x;
public int count;
public Point(int x,int y){
this.x=x;
this.count=y;
}
public Point(){}
}
public class Main{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int k=in.nextInt();
boolean vis[]=new boolean[k*2];
if(n>=k)System.out.println(n-k);
else
System.out.println(bfs(n,k,vis));
}
private static int bfs(int xx, int k,boolean vis[]) {
Queue<Point> q=new LinkedList<Point>();
Point p=new Point();
p.x=xx;
p.count=0;
vis[p.x]=true;
q.add(p);
while(!q.isEmpty()){
Point n=q.remove();
if(n.x>=2*k||n.x<0)continue;
if(n.x==k){
return n.count;
}else{
for(int i=0;i<3;i++)
{
Point n1=new Point();
if(i==0){
n1.x=n.x+1;
}
else if(i==1)
n1.x=n.x-1;
else
n1.x=2*n.x;
if((!(n1.x>=2*k||n1.x<0))&&vis[n1.x]==false)
{
vis[n1.x]=true;
n1.count=n.count+1;
q.add(n1);
}
}
}
}
return 0;
}
}
POJ3278_Catch That Cow(JAVA语言)的更多相关文章
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
- Atitit onvif协议获取rtsp地址播放java语言 attilx总结
Atitit onvif协议获取rtsp地址播放java语言 attilx总结 1.1. 获取rtsp地址的算法与流程1 1.2. Onvif摄像头的发现,ws的发现机制,使用xcf类库1 2. 调用 ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- Java语言中的面向对象特性总结
Java语言中的面向对象特性 (总结得不错) [课前思考] 1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类? 2. 面向对象编程的特性有哪三个?它们各自又有哪些特性? 3. 你知 ...
- JAVA语言搭建白盒静态代码、黑盒网站插件式自动化安全审计平台
近期打算做一个插件化的白盒静态代码安全审计自动化平台和黑盒网站安全审计自动化平台.现在开源或半开源做黑盒网站安全扫描的平台,大多是基于python脚本,安全人员贡献python脚本插件增强平台功能.对 ...
- 关于Java语言和面向对象记录
本科时常用的c语言是面向过程的语言,而Java是面向对象的语言 Java语言的11个关键术语 简单性.可移植性.面向对象.分布式.高性能.解释型.健壮性.多线程.安全性.动态性.体系结构中立 面向对象 ...
- 用Java语言编写一个简易画板
讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...
- 【百度文库课程】Java语言基础与OOP入门学习笔记一
一. Java的历史与由来 原名Oak,针对嵌入式系统开发设计,语法与C/C++基本一致 二. Java语言特点 Java由四方面组成:Java编程语言.Java类文件格式.Java虚拟机和Java应 ...
- 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词
第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...
随机推荐
- docker部署LNMP架构
环境要求: IP hostname 192.168.1.1 node1 项目规划: 容器网段:172.16.10.0/24 NGINX:172.16.10.10 MySQL:172.16.10.20 ...
- npm ci All In One
npm ci All In One npm 性能优化 npm ci 使用干净的面板安装项目 https://docs.npmjs.com/cli/v6/commands/npm-ci # npm cl ...
- free open music API all in one
free open music API all in one music API SoundCloud xgqfrms · free https://w.soundcloud.com/player/? ...
- Virtual Reality In Action
Virtual Reality In Action VR WebXR immersive 沉浸式 https://github.com/immersive-web/webxr https://imme ...
- GitHub Classroom
GitHub Classroom GitHub Education https://classroom.github.com/classrooms https://classroom.github.c ...
- github & markdown & image layout
github & markdown & image layout css & right https://github.com/sindresorhus/log-symbols ...
- vue $emit bug
vue $emit bug https://www.cnblogs.com/xgqfrms/p/11146189.html solution https://forum.vuejs.org/t/emi ...
- 黑马来袭!NGK生态所二月上线!
日前,加密货币交易所Coinbase Global Inc,向美国证券交易委员会申请首次公开募股,成为首家公开上市的加密货币交易所."Coinbase上市,给行业带来更多的是信心.让大家看到 ...
- SpringBoot Test 多线程报错:dataSource already closed
1:前言 最近在项目中使用多线程对大任务拆分处理时,进行数据库操作的时候报错了. 业务代码大概是这样的: @Service public calss TestServiceImpl implement ...
- Python爬虫系统化学习(2)
Python爬虫系统学习(2) 动态网页爬取 当网页使用Javascript时候,很多内容不会出现在HTML源代码中,所以爬取静态页面的技术可能无法使用.因此我们需要用动态网页抓取的两种技术:通过浏览 ...