Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9254    Accepted Submission(s): 2906


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



题目要求是让你找直线上从a点到b点的最短距离,没有那么简单,是有条件的,要满足这两个条件:

* 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.//距离是上一段距离的2倍;

程序写好后,提交时发现编译错误,经过查找,发现结构体变量stu now,next;从全局变量变成函数bfs里的变量后在杭电上提交正确


#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
int a[200005];
struct stu{
	int on;
	int step;
};
//struct node
//{
//    int on, step;
//    friend bool operator < (node a, node b)
//    {
//        return a.on > b.on; 
//    }
//};
//priority_queue<node>q;
queue<stu>q;
//priority_queue<stu>q;
//int M=100004;
//int IN=1000000;
int bfs()
{ 
    stu now,next;
	while(!q.empty())
	q.pop();
	now.on=n;
	now.step=0;
	a[now.on]=1;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
//		next.on=now.on;
//		next.step=now.step;
		for(int i=0;i<3;i++)
		{
			if(i==0)
			next.on=now.on+1;
			if(i==1)
			next.on=now.on-1;
			if(i==2)
			next.on=now.on*2;
			next.step=now.step+1;
			if(next.on==k)
			return next.step;
			if(next.on<0||next.on>100000)
			continue;
			if(!a[next.on])
			{
				a[next.on]=1;
				q.push(next);
			}
		}
	}
}
int main()
{
	int h;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		memset(a,0,sizeof(a));
		if(n<k)
		{
			h=bfs();
			printf("%d\n",h);
		}
		if(n==k)
		printf("0\n");
		if(n>k)
		printf("%d\n",n-k);
	}
	return 0;
}


再贴一个简单代码:

#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
int a[200005];
struct stu{
	int on;
	int step;
};
int bfs(int n,int k)
{
     queue<stu>q;
	 stu now,next;
	now.on=n;
	now.step=0;
	a[now.on]=1;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.on==k)
			return now.step;
		for(int i=0;i<3;i++)
		{
			if(i==0)
			next.on=now.on+1;
			if(i==1)
			next.on=now.on-1;
			if(i==2)
			next.on=now.on*2;
			
			if(a[next.on]||next.on<0||next.on>100000)
			continue;
				a[next.on]=1;
			  next.step=now.step+1;
			q.push(next);
		}
	}
}
int main()
{
	int h;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		memset(a,0,sizeof(a));

			h=bfs(n,k);
			printf("%d\n",h);
	}
	return 0;
}


你可能感兴趣的:(Catch That Cow)