poj 3278

poj 3278 catch that cow

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

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

#include <iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
bool vis[10005];
struct node
{
    int x;
    int step;
};
bool check(int x)
{
    if(x<0||x>10000||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;
    queue<node>q;
    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<<tmp<<endl;
    return 0;
}

poj 1426 Find the Multiple

pop掉一个数,塞进去两个衍生的数。
tips:
1.m的十进制表示不能超过100位。longlong的范围是肯定能找到一个符合条件的数,所以不要用字符串类型去表示100位。

#include <iostream>
#include<queue>

using namespace std;
int n;
void bfs(long long tmp)
{
    queue<long long>q;
    q.push(tmp);
    while(!q.empty())
    {
        long long now=q.front();
        q.pop();
        if(now%n==0)
        {
            cout<<now<<endl;
            return;
        }
        q.push(now*10);
        q.push(now*10+1);
    }
}
int main()
{
    while(cin>>n&&n)
    {
        bfs(1);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值