BFS POJ 3278 Catch That Cow

题目传送门

 /*
BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int d[MAXN];
bool vis[MAXN]; void BFS(void)
{
memset (vis, , sizeof (vis));
for (int i=; i<=1e6; ++i) d[MAXN] = ; queue<int> q;
q.push (n); d[n] = ; vis[n] = true; while (!q.empty ())
{
int x = q.front (); q.pop ();
if (x == m) break; int xl = x - ; int xr = x + ; int x2 = x * ; if (xl >= && !vis[xl])
{
q.push (xl); d[xl] = d[x] + ;
vis[xl] = true;
}
if (xr <= 1e6 && !vis[xr])
{
q.push (xr); d[xr] = d[x] + ;
vis[xr] = true;
}
if (x2 <= 1e6 && !vis[x2])
{
q.push (x2); d[x2] = d[x] + ;
vis[x2] = true;
}
} } int main(void) //POJ 3278 Catch That Cow
{
//freopen ("POJ_3278.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
BFS ();
printf ("%d\n", d[m]);
} return ;
}
上一篇:Google谷歌推出goo.gl缩短网址服务 - Blog透视镜


下一篇:git 入门学习笔记