您当前的位置: 首页 > 

钟钟终

暂无认证

  • 2浏览

    0关注

    233博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

poj 3278

钟钟终 发布时间:2021-07-26 18:06:13 ,浏览量:2

poj 3278 catch that cow

思路:维护一个队列,每个位置都有三种走法,然后将这三个状态放入队列中,再pop掉上一个位置。

不知道为啥,代码poj上过不了,感觉没什么问题,思路也很清晰

#include 
#include
#include
using namespace std;
int n,k;
bool vis[10005];
struct node
{
    int x;
    int step;
};
bool check(int x)
{
    if(x10000||vis[x])
        return false;
    else
        return true;
}
int bfs(int n,int k)
{
    node now,nt;
    vis[n]=1;
    now.x=n;
    now.step=0;
    queueq;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==k)
            return now.step;

        nt.x=now.x+1;
        if(check(nt.x))
        {
            nt.step=now.step+1;
            vis[nt.x]=1;
            q.push(nt);
        }

        nt.x=now.x-1;
        if(check(nt.x))
        {
            nt.step=now.step+1;
            vis[nt.x]=1;
            q.push(nt);
        }

        nt.x=now.x*2;
        if(check(nt.x))
        {
            nt.step=now.step+1;
            vis[nt.x]=1;
            q.push(nt);
        }
    }
}
int main()
{
    cin>>n>>k;
    memset(vis,0,sizeof(vis));
    int tmp=bfs(n,k);
    cout            
关注
打赏
1664378814
查看更多评论
0.0376s