HDU 2717 Catch That Cow

简单的广搜:

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

}

 

你可能感兴趣的:(catch)