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

poj 3278(bfs)

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

标签:namespace   iostream   logs   names   sizeof   eof   scan   span   main   

一条线上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)

标签:namespace   iostream   logs   names   sizeof   eof   scan   span   main   

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

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