poj 3278 Catch That Cow(bfs+队列)
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
题意:FJ要抓奶牛,输入n(FJ的位置),k(奶牛的位置),求FJ抓到奶牛所需最少时间。
FJ有三种走法:
1:向前移动一步,花费一分钟
2:向后退后一步,花费一分钟
3:向前移动当前位置的2倍,花费一分钟
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAX=;
bool vis[MAX];
int step[MAX];
queue <int >q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
step[n]=;
vis[n]=true;
while(!q.empty())
{
head=q.front(); //取队首
q.pop();
for(int i=; i<; i++)
{
if(i==)
next=head-;
if(i==)
next=head+;
if(i==)
next=head*;
if(next<||next>=MAX) //排除出界情况
continue;
if(!vis[next])
{
q.push(next); //入队
step[next]=step[head]+;//步数加一
vis[next]=true; //标记访问
}
if(next==k)
return step[next];
} }
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(vis,false,sizeof(vis));
memset(step,,sizeof(step));
while(!q.empty())
q.pop();
if(n>=k)
cout<<n-k<<endl;
else
cout<<bfs(n,k)<<endl;
}
return ;
}
poj 3278 Catch That Cow(bfs+队列)的更多相关文章
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 难度: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 ...
随机推荐
- CALL transaction 的用法-传内表
使用memory (这个方法和第二种方式的区别是可以传输复选框的值) data: wfbomcom type rc29n. move-corresponding bom_key to wfbomc ...
- jquery.form.js 使用以及问题(表单异步提交)
标注:我引用的js后报错 原因:是引用的js有冲突 我引用了两便jQuery: 转载:https://blog.csdn.net/cplvfx/article/details/80455485 使用方 ...
- iOS.GetCurrentTimestamp
Cocoa 中测量时间的方法 1. The Methods to Get Current Timestamp iOS中获取时间戳的方法: A. CACurrentMediaTime() B. gett ...
- 发送邮件--MFMailComposeViewController
只能在真机使用. 模拟器没有E-mail发送功能.无法调用 #import "EmailViewController.h" #import <UIKit/UIKit.h> ...
- Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1
C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...
- UVA-1364.Knights of the Round Table 无向图BCC
题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...
- Loadrunner 脚本录制策略
Loadrunner在脚本录制过程中,我们会先后分别碰见init.action.transaction.end.block等概念.本次打算以图文并茂的形式为大家分别讲解. 以下为一个简要的网站操作逻辑 ...
- Javascript的一个怪现象
javascript有一个怪现象,就是减法也会导致小数位数问题,是一个麻烦的问题,比如. <html><script> var a=10,b=20.1; alert( a - ...
- UI设计教程:如何在设计中运用颜色
灰度优先 我们习惯在设计阶段的早期就开始调整颜色和色调.但是,当你意识到自己花了3个小时来调整主色调的时候,你发现这种行为毫无帮助.虽然把玩颜色很有吸引力,但是你应该避免在设计初期进行这种行为. 相反 ...
- linux安全分析
history 查看历史命令 last | grep -i norco //最后一次登录时间