题目链接

http://poj.org/problem?id=3278

题意

给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2

求 最少的操作次数 使得 N 变成 K

思路

BFS 但是要注意 设置 数组的范围 小心 RE

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int MOD = 1e9 + 7; int n, k; int ans; int v[maxn]; struct node
{
int v, step;
}; bool ok(int x)
{
if (x < 0 || x > maxn)
return false;
return true;
} void bfs()
{
CLR(v, 0);
queue <node> q;
node tmp;
tmp.v = n;
tmp.step = 0;
q.push(tmp);
v[n] = 1;
while (!q.empty())
{
node u = q.front(), V;
q.pop();
if (u.v == k)
{
ans = u.step;
return;
}
V.step = u.step + 1;
V.v = u.v + 1;
if (ok(V.v) && v[V.v] == 0)
{
q.push(V);
v[V.v] = 1;
}
V.v = u.v - 1;
if (ok(V.v) && v[V.v] == 0)
{
q.push(V);
v[V.v] = 1;
}
V.v = u.v * 2;
if (ok(V.v) && v[V.v] == 0)
{
q.push(V);
v[V.v] = 1;
}
}
} int main()
{
scanf("%d%d", &n, &k);
ans = INF;
bfs();
cout << ans << endl;
}

POJ - 3278 Catch That Cow 【BFS】的更多相关文章

  1. POJ 3278 Catch That Cow【BFS】

    题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...

  2. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  3. hdoj 2717 Catch That Cow【bfs】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  5. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  6. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  7. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  8. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  9. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

随机推荐

  1. quick-cocos2d 设置横屏

    quick cocos2d新建项目,在xcode中 起模拟器,默认的是竖屏,我想做一个横屏的游戏,前面已经说了 选中你的项目,在General这个标签内,Deoployment info的这个分组,有 ...

  2. cordova热更新插件调试

    有更新www目录内容后,首先sencha app build,然后进入 cordova目录 运行 cordova-hcp build, 然后查看 chcp.json文件时间,然后压缩cordova目录 ...

  3. [转]MySQL的简单使用和JDBC示例

    MySql简单操作 //启动mysql net start mysql //登陆 mysql -u root -p //创建建数据库 create database mydb; create data ...

  4. Solidworks如何改变零件颜色

    如图所示装配体有三个零件,现在我想把移动件的颜色变成红色   鼠标左键单击要改变颜色的零件(这里点击"移动件"),然后在弹出的菜单中选择颜色,最后点击"编辑颜色" ...

  5. vue class绑定方式

    1.对象语法 <div class="static" v-bind:class="{ active: isActive, 'text-danger': hasErr ...

  6. WinForm搭载ScintillaNET时文本由于发生偏移被隐藏解决方案

    项目用ScintillaNet搭载到WinForm以满足文本编辑的需求,在用FindReplace.Scintilla.Text=“显示内容”输出文本内容的时候会碰到文本被WinForm边框隐藏的情况 ...

  7. 怎样给filter加入自己定义接口

    .在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...

  8. Html5 meta 笔记

    摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...

  9. 某个时间段关闭API

    def bet(): if 000000 < int(time.strftime("%H%M%S", time.localtime())) < 90000: print ...

  10. dede频道标签channel和频道内容标签channelartlist的调用栏目名的不同方式,如果错误使用标签会发生错误

    频道标签 [field:typename/] 频道内容标签 {dede:field name='typename'/}