首页 > 其他 > 详细

HDU 2717 Catch That Cow

时间:2014-02-09 16:27:31      阅读:267      评论:0      收藏:0      [点我收藏+]

简单的广搜:

bubuko.com,布布扣
#include <cstdio>
#include <queue>
using namespace std;

int map[200005],step[200005];
int n,start,end,res;

bool check(int x)
{
    if ((x>0)&&(x<200002)&&(map[x])) return true;
    else return false;
}

int bfs()
{
    int temp;
    if(start==end) return res;
    queue<int> Q;
    Q.push(start);
    step[start]=0;
    map[start]=false;
    while(!Q.empty())
    {
        temp=Q.front();
        Q.pop();
        if (temp+1==end) return step[temp]+1;
        if (temp-1==end) return step[temp]+1;
        if (temp*2==end) return step[temp]+1;
        if (check(temp+1))
        {
            int now=temp+1;
            Q.push(now);
            step[now]=step[temp]+1;
            map[now]=false; 
        }
        if (check(temp-1))
        {
            int now=temp-1;
            Q.push(now);
            step[now]=step[temp]+1;
            map[now]=false; 
        }
        if (check(temp*2))
        {
            int now=temp*2;
            Q.push(now);
            step[now]=step[temp]+1;
            map[now]=false; 
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d",&start,&end)!=EOF)
    {
        for(int i=1;i<200002;i++)map[i]=true;
        res=0; res=bfs();
        printf("%d\n",res);
    }
    return 0;
}
bubuko.com,布布扣

HDU 2717 Catch That Cow

原文:http://www.cnblogs.com/forever97/p/3541282.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!