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

题意:给你一个n和k,n可以加1也可以减1,还可以乘2,现在要求n经过这样的几步变换可以使得n==k;求得最小的步数。
题解:一开始我也不知道怎么办,准备用深度优先搜,但是自己没打出来,后来看了网上题目归类是BFSBFS,马上懂了,就是一个3入口的BFS。裸题。不过要注意0*2的情况。

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n,k;
int counts[];
struct Node {
int x;
int step;
};
int BFS(int x){
for(int i=;i<=;i++)
counts[i]=;
counts[x]=;
queue<Node>Q;
Node tt;
tt.x=x;
tt.step=;
Q.push(tt);
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
int xx=temp.x;
int step=temp.step;
if(xx+<=&&step+<counts[xx+]){
counts[xx+]=step+;
Node ttt;
ttt.x=xx+;
ttt.step=step+;
Q.push(ttt);
}
if(xx->=&&step+<counts[xx-]){
counts[xx-]=step+;
Node ttt;
ttt.x=xx-;
ttt.step=step+;
Q.push(ttt);
}
if(xx!=&&*xx<=&&step+<counts[xx*]){//注意这里的xx不等于0的判断
counts[xx*]=step+;
Node ttt;
ttt.x=xx*;
ttt.step=step+;
Q.push(ttt);
}
}
return counts[k];
}
int main(){
scanf("%d%d",&n,&k);
printf("%d\n",BFS(n)); }

Catch That Cow的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

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

  2. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  3. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

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

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

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  6. 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 ...

  7. BFS POJ 3278 Catch That Cow

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

  8. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  9. [HDOJ2717]Catch That Cow

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

  10. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

随机推荐

  1. Node.js小Httpserver

    须要说明两点: 1 程序文件hello.js需用记事本另存为utf-8格式的hello.js watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVhcGVk ...

  2. iOS-iPad开发之SplitViewController简单介绍

    iOS-iPad开发之SplitViewController简单介绍 SplitViewController图形化创建 SplitViewController可以并列显示两个view,适用于基于nav ...

  3. 第三篇:python基础之编码问题

    python基础之编码问题   python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode---&g ...

  4. spring事务回滚无法捕捉

    这篇文章讲解了怎么配置才能让spring事务捕捉异常 http://www.360doc.com/content/12/1109/18/6161903_246870991.shtml 需要正确配置sp ...

  5. android 定时请求(两种实现方式)

    方式一: Handler + Runnable (借鉴网址:http://stackoverflow.com/questions/6207362/how-to-run-an-async-task-fo ...

  6. Android开发---支付宝功能接口(支付功能)(转载!)

    最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...

  7. MongoDB_1

    突然想去看下MongoDB的东西,于是有了这篇文章.其实很早以前就看过一些关于NoSql的文章,还记得当时里面有介绍MongoDB的,多瞅了2眼,并且在Window下安装了MongoDB的驱动,小玩了 ...

  8. iOS NSData简单解析

    iOS 基本数据类型之NSData 1 nsdata 作用: 用于存储二进制的数据类型 nadat类提供一种简单的方式,它用来设置缓存区.将文件的内容读入到缓存区.或者将缓存区中的内容写到一个文件. ...

  9. 那些年,我们一起被坑的H5音频

    原文地址:http://weibo.com/p/23041874d6cedd0102vkbr   不要被这么文艺的标题吓到,这里不会跟你讲述中学时期泡妞史,也不会有其它什么现实不该有而小说噼里啪啦不能 ...

  10. php基础知识【函数】(4)时间date

    一.time() -- 返回当前的 Unix 时间戳 $nextWeek = time() + (7 * 24 * 60 * 60); echo 'Next Week: '. date('Y-m-d' ...