Catch That Cow【广搜】
Catch That Cow
POJ - 3278
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.
依旧是广搜的模板题
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
/*priority_queue<int,vector<int> ,greater<int> >q;*/
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
bool vis[maxn];
int step[maxn];
int bfs(int s,int e)
{queue<int> q;q.push(s);step[s]=0;vis[s]=true;while(!q.empty()){int front=q.front();q.pop();if(front+1>0&&front+1<=1e5&&vis[front+1]==false){q.push(front+1);vis[front+1]=true;step[front+1]=step[front]+1;if(front+1==e)return step[front+1];}if(front-1>0&&front-1<=1e5&&vis[front-1]==false){q.push(front-1);vis[front-1]=true;step[front-1]=step[front]+1;if(front-1==e)return step[front-1];}if(front*2>0&&front*2<=1e5&&vis[front*2]==false){q.push(front*2);vis[front*2]=true;step[front*2]=step[front]+1;if(front*2==e)return step[front*2];}}
}
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int N,K;cin>>N>>K;ms(vis);ms(step);if(N>=K)cout<<N-K<<endl;elsecout<<bfs(N,K)<<endl;return 0;
}
Catch That Cow【广搜】相关推荐
- Catch That Cow——广搜
Catch That Cow Problem Description Farmer John has been informed of the location of a fugitive cow a ...
- poj 3278 Catch That Cow 广搜
hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- POJ 3728 Catch That Cow (广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42564 Accepted: 13225 ...
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...
- poj 3278 Catch That Cow(广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45087 Accepted: 14116 ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Catch That Cow抓住那只奶牛(BFS,广度优先搜索)
# **Catch That Cow(抓住那只奶牛)** [题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个 ...
- 深搜广搜专题【DFS】【BFS】
深搜广搜专题 又是一年专题时,这次的专题是BFS和DFS,我们刚加入acm时的噩梦,然而现在已经写起来很舒服了(OS:那你还A不出题?) BFS和DFS都是通过对所有的点进行遍历来得到结果的,是一种比 ...
- hdu 2717 Catch That Cow
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
最新文章
- AI 八数码A_star算法问题-实验报告
- 如何通过日期属性对数组进行排序
- 不讲武德的微信,又来一波新功能!
- html如何让左右字段不能编辑,HTML中让表单input等文本框为只读不可编辑的方法...
- 探究防抖(debounce)和节流(throttle)
- Leetcode5634. 删除子字符串的最大得分[C++题解]:贪心
- Android项目实战(十三):浅谈EventBus
- nagios报Connection refused by host的解决办法
- 书籍推荐-记这几年看的书
- numpy 数组与矩阵的乘法理解
- 一分钟实现纯CSS全屏滚动特效
- 如何在自己开发的日程管理页面插入提醒功能_给大家推荐一款高颜值的日程管理类APP...
- Again Twenty Five!
- PHP抖音无水印解析视频代码+思路
- Android - MTK系统源码结构
- 时间工具类封装以及时间戳之间的相互转换
- Python批量导入图片到Word文件
- 背阔肌(06):T杠俯身划船
- grafana是什么?
- JAVA权重算法(如Dubbo的负载均衡权重)
热门文章
- qt的项目中单个文件加载样式表
- CTF-MISC杂项题2
- oracle修改窗口字体大小,jQuery之字体大小的设置方法
- opensips mysql 认证_基于ubuntu中使用mysql实现opensips用户认证的解决方法
- php ldap 模块,不重新编译为PHP增加LDAP模块的支持
- HTML计算机英文字母,计算机入门知识 英文字母缩写翻译 -电脑资料
- HTML用css让input无法使用,html – 是否可以使用CSS设置一个禁用的INPUT元素?
- java+set+split_阿里资深工程师教你如何优化 Java 代码!
- bson json c语言,对比平台--JSON和BSON之间的区别
- php数据回显是什么意思,jquery回显是什么意思