码迷,mamicode.com
首页 > 其他好文 > 详细

Catch That Cow 杭电2717【BFS】

时间:2015-08-06 13:26:51      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

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是万能的。暂时还没找到不能用的情况,可能做的题目太少了吧

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

Catch That Cow 杭电2717【BFS】

标签:

原文地址:http://blog.csdn.net/yuzhiwei1995/article/details/47312941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!