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 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

题意:人在n位置处,牛在k位置处,牛不动问人最短多少分钟可以捉到牛,人有两种移动方法

1、一分钟移动一步n-1或者n+1

2、一分钟移动2*n步;

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 1000100
using namespace std;
int n,m;
int vis[MAX];
struct node
{
	int x;
	int step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
};
int judge(int x)
{
	if(x < 0||x > MAX||vis[x])
	    return 0;
	return 1;
}
void bfs(int n,int k)
{
	int i,j;
	priority_queue<node>q;
	node beg,end;
	beg.x=n;
	beg.step=0;
	q.push(beg);
	vis[n]=1;
	while(!q.empty())
	{
		beg=q.top();
		q.pop();
		if(beg.x==k)
		{
			printf("%d\n",beg.step);
			return ;
		}
		end.x=beg.x-1;
		if(judge(end.x))
		{
			vis[end.x]=1;
			end.step=beg.step+1;
			q.push(end);
		}
		end.x=beg.x+1;
		if(judge(end.x))
		{
			vis[end.x]=1;
			end.step=beg.step+1;
			q.push(end);
		}
		end.x=beg.x*2;
		if(judge(end.x))
		{
			vis[end.x]=1;
			end.step=beg.step+1;
			q.push(end);
		}
	}
}
int main()

{
	int i;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		bfs(n,m);
	}
	return 0;
}

  

时间: 2024-10-11 01:25:39

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

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 c

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的