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. Android传感器概述(六)

    监视传感器事件 要监视原始的传感器数据,你须要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged().Andr ...

  2. Web classPath

    classpath,看名字,类路径,这样比如,对于java程序,就是告诉java程序哪里去找类.(java虚拟机都是通过类装载器的)想myeclipse中struts,spring,hibernate ...

  3. MySQL的优化点总结---通过计算多种状态的百分比看MySQL的性能情况

    1 读写比例: show global status like 'com_select';  获得服务器启动到目前查询操作执行的次数:show global status like 'com_inse ...

  4. mysql内核源代码深度解析 缓冲池 buffer pool 整体概述

    http://blog.csdn.net/cjcl99/article/details/51063078

  5. 源码解析之–网络层YTKNetwork

    首先 关于网络层最先可能想到的是AFNetworking,或者Swift中的Alamofire,直接使用起来也特别的简单,但是稍复杂的项目如果直接使用就显得不够用了,首先第三方耦合不说,就光散落在各处 ...

  6. cookingk配置项中的chunk:true含义

    学习cooking,查看中文文档,发现有个配置chunk: true, 不明白具体含义,于是根据注释,找到了相关代码, 即chunk: true等同于以下代码: var webpackConfig = ...

  7. HTML5 文件域+FileReader 读取文件(二)

    一.读取文本文件内容,指定字符编码 <div class="container"> <!--文本文件验证--> <input type="f ...

  8. 利用ASP.NET AJAX的Timer讓GridView每隔一段時間做到自動換頁的功能

    最近在討論區看到這個問題,小弟利用asp.net ajax的timer來實作這個功能 利用timer每隔一段時間,讓gridview自動跳頁並且更新gridview的內容 asp.net(c#) Gr ...

  9. Visual C#实现Windows信使服务

    现在有很多网络管理软件都具备网络上信息实时传送的功能,虽然有些网络通讯软件功能比较强大,有的软件不仅可以传送文本信息,还可以传送二进制文件等.但 它们都有一个无法克服的缺点,那就是分发比较困难,信息传 ...

  10. Visual Studio2010 安装msdn

    1.注册VS2010 断网(不要冒险)->运行Microsoft Visual Studio 2010->帮助->注册产品->YCFHQ-9DWCY-DKV88-T2TMH-G ...