poj3278 BFS入门
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=4e5+5;
int n,m;
queue<pair<int,int> >q;
bool hash[maxn];
int main()
{
int m,n;
while(cin>>n>>m)
{
memset(hash,false,sizeof(hash));
pair<int,int> p;
p.first=n;
p.second=0;
hash[n]=true;
q.push(p);
while(!q.empty())
{
pair<int,int> a,b;
a=q.front();
if(a.first==m)
{
cout<<a.second<<endl;
break;
}
a.second++;
b=a;
if(b.first>m)
{
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
}
if(b.first<m)
{
b.first=a.first+1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
b.first=a.first*2;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
}
q.pop();//弹出队列第一个元素
}
while(!q.empty())
q.pop();//注意清空队列
}
return 0;
}
poj3278 BFS入门的更多相关文章
- HDU1548- A strange lift (BFS入门)
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A Strrange lift Time Limit: 2000/1000 MS (Java/ ...
- POJ-3278(BFS)
题目: ...
- POJ 3278 抓奶牛(BFS入门题)
描述 农夫约翰已被告知逃亡牛的位置,并希望立即抓住她.他开始于一个点Ñ(0≤ Ñ ≤100,000)上的数线和牛是在点ķ(0≤ ķ上相同数目的线≤100,000).农夫约翰有两种交通方式:步行和传送. ...
- hdu 1242 Rescue(BFS入门)
第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...
- BFS入门
#include<iostream> #include<cstring> #include<queue> using namespace std; #define ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- HDU2717-Catch That Cow (BFS入门)
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/O ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj3278 【BFS】
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 97240 Accepted: 30519 ...
随机推荐
- Delphi文件映射
http://www.cnblogs.com/key-ok/p/3429860.htmlhttp://www.cnblogs.com/key-ok/p/3380793.htmlhttp://www.c ...
- 关于type check的定义
Concept: Type Checking There is no static type checking in Scheme; type checking is done at run time ...
- Annikken Andee–Arduino与Android间的简易连接
一个Arduino的兼容板,允许你显示并控制来自Android设备的Arduino应用.无需Anroid APP开发. 点击:观看视频 什么是Annikken Andee? Annikken Ande ...
- HDOJ(HDU) 2156 分数矩阵(嗯、求和)
Problem Description 我们定义如下矩阵: 1/1 1/2 1/3 1/2 1/1 1/2 1/3 1/2 1/1 矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增. 请求 ...
- poj1743 Musical Theme(后缀数组|后缀自动机)
[题目链接] http://poj.org/problem?id=1743 [题意] 求不可重叠最长重复子串. 2015-11-27 [思路] 1) 据题意处理字符串 ...
- [Locked] Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- Hibernate五 HQL查询
HQL查询一 介绍1.HQL:Hibernate Query Language,是一种完全面向对象的查询语言.使用Hibernate有多重查询方式可供选择:hibernate的HQL查询,也可以使用条 ...
- Java 流的概述及操作(转)
一.什么是流? 流就是字节序列的抽象概念,能被连续读取数据的数据源和能被连续写入数据的接收端就是流,流机制是Java及C++中的一个重要机制,通过流我们可以自由地控制文件.内存.IO设备等数据的流向. ...
- Linux用户root忘记密码的解决(unbuntu16.04)
参考: http://www.linuxidc.com/Linux/2012-04/59069.htm http://www.68idc.cn/help/server/linux/2015060735 ...
- Hibernate命名空间怎样实现?
什么是命名查询? Hibernate同意在映射文件里定义字符串形式的查询语句.这样的查询方式成为命名查询 使用命名查询有什么优点? 因为使用Hibernate的HQL经常须要在Java代码中写字 ...