POJ 3278

Posted jrfr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3278相关的知识,希望对你有一定的参考价值。

BFS

1:队列stl

技术图片
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
using namespace std;
//int n,m;
int vis[100010],step[100010];
queue<int >q;
int  bfs(int n,int m)
{
    int head, next;
    q.push(n);
    step[n]=0;
    vis[n]=1;

    while(!q.empty())
    {
        head=q.front();

        for(int i=0;i<3;i++)
        {
            if(i==0)
            {
                next=head-1;
            }
            else if(i==1)
            {
                next=head+1;
            }
            else
            {
                next=head*2;
            }
            if(next<0 || next>100000) continue;

            if(!vis[next])
            {
                q.push(next);
                step[next]=step[head]+1;
                vis[next]=1;
            }
            if(next==m)
            {
                return step[next];
            }
        }
        q.pop();
    }
}

int main()
{
    int n,m;

    while(scanf("%d%d", &n, &m)!=EOF)
    {
        memset(step, 0, sizeof(step));
        memset(vis, 0, sizeof(vis));

        while(!q.empty()) q.pop();

        if(n>=m)
            printf("%d
", n-m);
        else
            printf("%d
", bfs(n, m));
    }
}
View Code

2:模拟duilie

技术图片
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
using namespace std;
int b[110000];
int step[110000],q[110000];
int n,m,head,tail,l,r,step1,step2;
queue<int>que;
int BFS(int n,int m)
{
    head=1,tail=1;
    q[head]=n;

    while(1)
    {
        step1=q[head];
        for(int i=1;i<=3;++i)
        {
            if(i==1)
            {
                step2=step1+1;
            }
            else if(i==2)
            {
                step2=step1-1;
            }
            else if(i==3)
            {
                step2=step1*2;
            }
            if(step2<0 || step2>100000) continue;
            if(b[step2]==0)
            {
                b[step2]=1;
                tail++;
                q[tail]=step2;
                step[step2]=step[step1]+1;
            }
            if(step2==m)
            {
                return step[step2];
            }
        }
        head++;
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(b,0,sizeof(b));
        memset(step,0,sizeof(step));
        memset(q,0,sizeof(q));
        //while(!que.empty()) que.pop();
        if(n>=m)
       {
           cout<<n-m<<endl;
       }
       else
       {
           cout<<BFS(n,m)<<endl;
       }
    }


}
View Code

 

以上是关于POJ 3278的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3278 Catch That Cow(简单BFS)

POJ3278_Catch that cow

poj3278 Catch That Cow

POJ3278:Catch That Cow

POJ 3278 Catch That Cow

POJ 3278 Catch That Cow