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

题意:

给定两个整数n和k

通过 n+1或n-1 或n*2 这3种操作,使得n==k

输出最少的操作次数

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[];
struct node
{
int x,step;
};
int bfs(int n,int k)
{
if(n==k)
return ;
queue<node>q;
struct node next,pos;
next.step=; next.x=n;
vis[n]=;
q.push(next);
while(!q.empty())
{
next=q.front();
q.pop();
if(vis[next.x-]==&&(next.x-)>=&&(next.x-)<=)
{
pos.x=next.x-; pos.step=next.step+;
q.push(pos);
vis[next.x-]=;
if(pos.x==k)
return pos.step;
}
if(vis[next.x+]==&&(next.x+)>=&&(next.x+)<=)
{
pos.x=next.x+; pos.step=next.step+;
q.push(pos);
vis[next.x+]=;
if(pos.x==k)
return pos.step;
}
if(vis[next.x*]==&&(next.x*)>=&&(next.x*)<=)
{
pos.x=next.x*; pos.step=next.step+;
q.push(pos);
vis[next.x+]=;
if(pos.x==k)
return pos.step;
}
}
return ;
};
int main()
{
int n,k,sum;
while(~scanf("%d%d",&n,&k))
{
memset(vis,,sizeof(vis));
sum=bfs(n,k);
printf("%d\n",sum);
}
}

poj 3278 Catch That Cow (bfs)的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

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

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

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

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

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

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

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

  5. poj 3278(hdu 2717) Catch That Cow(bfs)

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

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  7. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  8. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  9. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

随机推荐

  1. Linux 查看某个用户的进程

    Linux 查看某个用户的进程 To view only the processes owned by a specific user, use the following command: top ...

  2. OFBiz进阶之HelloWorld(五)创建新实体

    参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guid ...

  3. ViewData,ViewBag和TempData

      ViewData ViewBag TempData 类型 字典 Dynamic TempDataDictionary 出生时间 MVC1 MVC3   框架版本 .net3.5 .net4.0   ...

  4. Linux的安全模式

    今天尝试了一下开机启动,在rc.local中进行设置,但是我写的java -jar transport.jar是一个Hold处理,无法退出:导致开机的时候一直停留在等待页面. 处理机制: 1. 在Li ...

  5. iOS 基础 第三天(0807)

    0807 成员变量作用域###### 如下图所示: 这里要注意手写的成员变量/实例变量默认的作用域是private,所以外部指针类型的对象无法直接访问,这起到一定的保护作用.但可以在当前类内部@imp ...

  6. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  7. swift基础--运算符

    (1)加减乘除 (2)三目运算,切记后面的空格如果不加会报错的.估计是苹果的bug. (3)聚合运算符,省却了一个判断,很人性化 (4)区间运算符 //加减乘除等等 let a = 2 let b = ...

  8. 现代浏览器原生js获取id号方法

    <div id="tests" class="a b c" style="color:#f00">123</div> ...

  9. WEB开发人员必知的20+HTML5技巧(转)

    互联网科技发展的速度真可谓惊人的快,一个稍不留神,你就可能无法跟上它的步伐. HTML5的变化和更新也压倒不少人,这篇文章将向大家介绍一些最基本也非常必要的 HTML技巧. 1. 新的文档类型(Doc ...

  10. jQuery列表拖动排列-jquery list dragsort插件参数和使用方法

    在编写网页的时候,有时可能需要对ul的li进行排序,今天就给大家推荐使用jquery插件jquery list dragsort实现列表拖动排序效果. 效果如图: jquery list dragso ...