码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3278

时间:2019-03-12 22:37:15      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ons   mat   step   sizeof   cstring   ret   ++i   main   fine   

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", n-m);
        else
            printf("%d\n", 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

标签:ons   mat   step   sizeof   cstring   ret   ++i   main   fine   

原文地址:https://www.cnblogs.com/jrfr/p/10520092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!