hdu2717 Catch That Cow
来源:互联网 发布:阿里云代理加盟 编辑:程序博客网 时间:2024/06/05 02:00
Catch That Cow
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX + 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.
大神思路:
农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上幽发,尽快把那只奶牛抓回来.
他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有
两种办法移动,步行和瞬移:步行每秒种可以让约翰从z处走到x+l或x-l处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.
那么,约翰需要多少时间抓住那只牛呢?
题解:
宽搜,判断是否出界以及是否被使用过(因为再次走到已经使用过的点一定不是最优解),然后把满足条件的店推进队列里。
在搜索专题中,我们经常用到两种算法,一种是DFS(深度优先搜索)和BFS(广度优先搜索)。
在搜索中 ,我们采用队列的形式,就是先进先出,就好比我们排队吃饭一样,我排在前面的,我就可以先打到饭。
在搜索中,我们讲符合条件的
点放进队列用,在取出来用的时候,就是先把放进去的先取出来使用。
本题我采用的是BFS算法。因为BFS算法比较通用。
在本题中,农场主有三种方式,一种是往前走一步,一种是往后走一步,还有一种是瞬移到原来位置的两倍。
在BFS循环的时候就可以将这三种
操作进行处理。 走过的位置标记。代表不会走回原来的位置。因为不标记的话,会导致死循环,爆内存。
在找到牛的时候跳出。
对所有状况进行一次搜索,最先找到的肯定就是时间最少的。
我的代码:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int n,k;
struct node
{
int x;
int time;
};
int mark[1000010];
int go(int x)
{
if(x<0||x>=1000000||mark[x])
return 0;
return 1;
}
int bfs(int x)
{
node st,ed;
queue<node>que;
st.x=x;
st.time=0;
mark[st.x]=1;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
if(st.x==k)
{
return st.time;
}
ed=st;
ed.x=st.x+1;
if(go(ed.x))
{
ed.time=st.time+1;
mark[ed.x] = 1;
que.push(ed);
}
ed.x=st.x-1;
if(go(ed.x))
{
ed.time=st.time+1;
mark[ed.x] = 1;
que.push(ed);
}
ed.x=st.x*2;
if(go(ed.x))
{
ed.time=st.time+1;
mark[ed.x] = 1;
que.push(ed);
}
}
return -1;
}
int main()
{
int ans;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(mark,0,sizeof(mark));
ans=bfs(n);
printf("%d\n",ans);
}
return 0;
}
- 【HDU2717】-Catch that cow
- HDU2717:Catch That Cow
- HDU2717 Catch That Cow
- HDU2717 Catch That Cow
- HDU2717 - Catch That Cow
- HDU2717 Catch That Cow
- HDU2717 Catch That Cow
- HDU2717 Catch That Cow
- hdu2717:Catch That Cow
- HDU2717 Catch That Cow
- hdu2717 Catch That Cow
- hdu2717 Catch That Cow
- hdu2717 Catch That Cow
- HDU2717:Catch That Cow(BFS)
- hdu2717 Catch That Cow(搜索)
- HDU2717 Catch That Cow BFS
- HDU2717:Catch That Cow(BFS)
- hdu2717 catch that cow【BFS】
- Oracle用sys_Guid()创建为视图ID,.net跨库保存的值却为system_Byte[]问题解决--将sys_Guid()转换为string
- 归一化
- FPGA电路逻辑的原理图方式设计与验证
- html之闭包函数应用
- LVS+keepalived+nginx反向代理
- hdu2717 Catch That Cow
- ViewPager+Fragment滑动
- 深入浅出nodejs内存控制知识点整理
- Eclipse无Server或者Tomcat8.5解决办法
- angularjs路由、排序、查询
- Tsinsen A1013 进制转换4
- 获取文件基本属性
- JQuery Ajax 回调函数为全局变量赋值
- 为什么网上鲜有炫富的程序员