首页 > 其他 > 详细

poj 3278(bfs)

时间:2017-08-30 22:14:33      阅读:280      评论:0      收藏:0      [点我收藏+]

一条线上bfs搜就行

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100000+100;
int n,k;
bool vis[maxn*2+100];
struct note
{
    int x;
    int cnt;
};
int bfs(int x,int y)
{
    queue<note> q;
    q.push(note{x,0});
    while(q.size())
    {
        note hh=q.front();
        q.pop();
         int mm=hh.cnt;
         int xx=hh.x;
         int aa;
         aa=xx+1;
        if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
         aa=xx-1;
         if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
         aa=xx*2;
        if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
    }
    return -1;

}
int main()
{
      while(~scanf("%d%d",&n,&k))
      {  memset(vis,0,sizeof(vis));
          if(n==k) printf("0\n");
          else
          {
              int mm=bfs(n,k);
               printf("%d\n",mm);
          }
      }
    return 0;
}

 

poj 3278(bfs)

原文:http://www.cnblogs.com/Wangwanxiang/p/7455451.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!