hdoj 2717 Catch That Cow 【bfs】

Catch That Cow

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 88   Accepted Submission(s) : 36

Problem Description

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

Source

PKU

分析:老头要去抓住他的牛,已经知道牛的位置,和自己的位置,老头可以前进一步或者退一步,或者传送到当前位置数字的二倍的位置。计算老头需要几步能抓住他的牛。

代码:

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

int n,m;
int vis[200000];

struct node{
	int x,y,step;
}pp;
queue<node>q;
int  bfs()
{
	while(!q.empty())
	{
		q.pop();
	}
	q.push(pp);
	memset(vis,0,sizeof(vis));
	vis[pp.x]=1;
	while(!q.empty())
	{
		node gg=q.front();
		if(gg.x==m)
		return gg.step;
		q.pop();
		for(int i=0;i<3;i++)
		{
			node dd=gg;
			if(i==0)
			dd.x=dd.x+1;
			else if(i==1)
			dd.x=dd.x-1;
			else if(i==2)
			dd.x=dd.x*2;
			dd.step++;
			if(dd.x==m)
			return dd.step;
			if(dd.x>=0&&dd.x<=200000&&!vis[dd.x])
			{
				vis[dd.x]=1;
				q.push(dd);
			}

		}
	}
	return 0;
//	memset(bis,0,sizeof(vis));
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
         pp.x=n;
		 pp.step=0;
		 int time=bfs();
		 printf("%d\n",time);
	}
	return 0;
}

版权声明:博主情人,外人误碰!!!

时间: 2024-10-09 18:37:12

hdoj 2717 Catch That Cow 【bfs】的相关文章

hdoj 2717 Catch That Cow【bfs】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9276    Accepted Submission(s): 2907 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

poj 3278 Catch That Cow 【bfs】

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52335   Accepted: 16416 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,00

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

hdoj 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9719    Accepted Submission(s): 3043 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow Problem 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

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的