POJ3278 HDU2717 Catch That Cow【BFS】

需要注意的是走过的点进行标注就不再继续走下去。否则会造成错无,同时需要判断这点在不在0-200000的范围内,并且判断条件在该点是否走过了的的前面。
ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
using namespace std;

struct node
{
    int x;
    int mint;
}cat;

const int MAXN = 100000;
const int MAXN2 = MAXN * 2;

bool book[MAXN2+2];
int n,k,mini;

void bfs()
{
    queue<node> q;
    cat.mint=0;
    cat.x=n;
    book[n]=true;
    q.push(cat);
    while(!q.empty())
    {
        node fro;
        fro=q.front();
        q.pop();
        if(fro.x==k)
        {
            mini=fro.mint;
            break;
        }
        node a,b,c;
        a.x=fro.x+1;
        a.mint=fro.mint+1;
        if(a.x<=200000&&!book[a.x])
        {
            q.push(a);
            book[a.x]=true;
        }


        b.x=fro.x-1;
        b.mint=fro.mint+1;
        if(b.x>=0&&!book[b.x])
        {
            q.push(b);
            book[b.x]=true;
        }


        c.x=fro.x*2;
        c.mint=fro.mint+1;
        if(c.x<=200000&&!book[c.x])
        {
            q.push(c);
            book[c.x]=true;
        }


    }
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);

    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(book,false,sizeof(book));
        bfs();
        cout<<mini<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值