Catch That Cow 杭电2717【BFS】

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.

//BFS

#include<cstdio>
#include<queue>
#include<cstring>
#define INF 0xfffffff
#define max 100000
using namespace std;
bool vis[max+100];
int min_time;
int tar;
struct cow
{
    int start,time;
}a,temp;

void BFS(int st)
{
    queue<cow>q;
    a.start=st;
    a.time=0;
    memset(vis,0,sizeof(vis));
    vis[a.start]=1;
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=1;i<=3;++i)
        {
            if(i==1) temp.start=a.start+1;
            else if(i==2) temp.start=a.start-1;
            else if(i==3) temp.start=a.start*2;
            temp.time=a.time+1;
            if(temp.start==tar)
            {
                if(min_time>temp.time)
                min_time=temp.time;
            }
            if(temp.start>max||temp.start<0) continue;

            if(!vis[temp.start])
            {
                vis[temp.start]=1;
                q.push(temp);
            }

        }
    }
}
int main()
{
    int st;
    while(~scanf("%d%d",&st,&tar))
    {
        if(st==tar)
        {
            printf("0\n");
            continue;
        }
        if(st>tar)
        {
            printf("%d\n",st-tar);
            continue;
        }
        min_time=INF;
        BFS(st);
        printf("%d\n",min_time);
    }
    return 0;
}

//用优先队列优化过后的BFS

#include<cstdio>
#include<queue>
#include<cstring>
#define INF 0xfffffff
#define max 100000
using namespace std;
bool vis[max+100];
int min_time;
int tar;
struct cow
{
	int start,time;
	friend bool operator <(cow a,cow b)
	{
		return a.time>b.time;
	}
}a,temp;

void BFS(int st)
{
	priority_queue<cow>q;
	a.start=st;
	a.time=0;
	memset(vis,0,sizeof(vis));
	vis[a.start]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		for(int i=1;i<=3;++i)
		{
			if(i==1) temp.start=a.start+1;
			else if(i==2) temp.start=a.start-1;
			else if(i==3) temp.start=a.start*2;
			temp.time=a.time+1;
			if(temp.start==tar)
			{
				min_time=temp.time;
				return ;
			}
			if(temp.start>max||temp.start<0) continue;

			if(!vis[temp.start])
			{
				vis[temp.start]=1;
				q.push(temp);
			}

		}
	}
}
int main()
{
	int st;
	while(~scanf("%d%d",&st,&tar))
	{
		if(st==tar)
		{
			printf("0\n");
			continue;
		}
		if(st>tar)
		{
			printf("%d\n",st-tar);
			continue;
		}
		min_time=INF;
		BFS(st);
		printf("%d\n",min_time);
	}
	return 0;
}

//DFS

我总结一下什么时候用BFS,什么时候用DFS,有有限边界的,比如给了一个图,就用DFS方便,如果边界太大1000000,就用BFS

不然会爆,因为运行太多1000000次出不来,这个程序想都不用想就不行。然而好像BFS是万能的。暂时还没找到不能用的情况,可能做的题目太少了吧

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 10:27:34

Catch That Cow 杭电2717【BFS】的相关文章

杭电 HDU 2717 Catch That Cow

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

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

[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 hdu 2717 Catch That Cow

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

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

HDU 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): 10575    Accepted Submission(s): 3303 Problem Description Farmer John has been informed of the location of a fugitive cow and want

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

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

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive