poj 3278 catch that cow BFS(基础水)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 61826 | Accepted: 19329 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
[Submit] [Go Back] [Status] [Discuss]
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
vis[]; struct node{
int x;
int dis; };
node u,v; int bfs(int start,int end){
u.x=start;
u.dis=;
queue<node>q;
vis[start]=true;
q.push(u);
while(!q.empty()){
u=q.front();
q.pop();
if(u.x==end)
return u.dis;
for(int i=;i<;i++){
if(i==)
v.x=u.x+;
else if(i==)
v.x=u.x-;
else if(i==)
v.x=u.x*;
if(!vis[v.x]&&v.x>=&&v.x<=){
vis[v.x]=true;
v.dis=u.dis+;
q.push(v);
}
} }
} int main(){
int start,end;
while(scanf("%d%d",&start,&end)!=EOF){
memset(vis,false,sizeof(vis));
int step=bfs(start,end);
printf("%d\n",step);
}
return ;
}
poj 3278 catch that cow BFS(基础水)的更多相关文章
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- poj 3278 Catch That Cow bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj 3278 Catch That Cow(bfs+队列)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- Google Guava入门(一)
Guava作为Java编程的助手,可以提升开发效率,对Guava设计思想的学习则极大的有益于今后的编程之路.故在此对<Getting Started with Google Guava>一 ...
- javaweb基础(30)_EL函数库
一.EL函数库介绍 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 这些EL函数在JSTL开发包中进行描述,因此在JSP页 ...
- JQuery从服务器端取得数据绑定到dropdownlist(select)中
http://blog.csdn.net/gaofang2009/article/details/5840783 http://www.cnblogs.com/Mac_Hui/archive/2010 ...
- BeyondCompare:如何之比较文件内容的不同?
问题描述: 在使用beyond compare比较文件的时候,常会有很多不同,但是点击打开后,发现内容没有不同.这个是因为工具把文件的日期.大小等非内容因素也比较了进去. 解决方法: 点击“会话” - ...
- runtime消息转发机制
Objective-C 扩展了 C 语言,并加入了面向对象特性和 Smalltalk 式的消息传递机制.而这个扩展的核心是一个用 C 和 编译语言 写的 Runtime 库.它是 Objective- ...
- 开发中经常遇到的一些css样式问题
1.经常会遇到子元素浮动了,父元素就会探索到一个合适的值现在值是0,就要清除浮动操 <div class="search clearfix"> <div clas ...
- 51nod——2478 小b接水(预处理 思维)
我本来想把每个谷都处理了,想了下觉得不好办.后来看其他人写的是处理每个位置,把每个位置可以接的水累加起来.整挺好. #include <bits/stdc++.h> using names ...
- 1072: [SCOI2007]排列perm
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3000 Solved: 1875[Submit][Status][Discuss] Descript ...
- (77)zabbix主动、被动检测的详细过程与区别
zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...
- 10分钟了解 react 引入的 Hooks
"大家好,我是谷阿莫,今天要将的是一个...",哈哈哈,看到这个题我就想到这个开头.最近react 官方在 2018 ReactConf 大会上宣布 React v16.7.0-a ...