Catch That Cow
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的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- 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 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 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 ...
- [HDOJ2717]Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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,最先 ...
随机推荐
- Linux Resin 安装
1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...
- [RxJS] Filtering operators: takeLast, last
Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...
- hdoj Last non-zero Digit in N! 【数论】
找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...
- Objective-C中的@Property详解
Objective-C中的@Property详解 @Property (属性) class vairs 这个属性有nonatomic, strong, weak, retain, copy等等 我把它 ...
- 关于Sublime Text3 pyV8无法加载的问题
昨天切换到sublime text 3 安装 emmet插件 不起作用 提示 pyv8 无法加载 手动下载安装解决 问题描述 PyV8 Binaries Archive of pre-compi ...
- CentOS 通过yum来升级php到php5.6,yum upgrade php 没有更新包怎么办?
在文章中,我们将展示在centOS系统下如何将php升级到5.6,之前通过yum来安装lamp环境,直接升级的话,提示没有更新包,也就是说默认情况下php5.3.3是最新 1.查看已经安装的php版本 ...
- Conversion Between DataTable and List in C#
1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...
- jquery ajax异步调用
写程序的第一步都要知其然,至于知其所以然就要看个人的爱好了.下面说一下web开发中经常用的ajax. 这里是用的jquery框架实现的ajax异步调用.废话少说先上代码.(asp.net开发) var ...
- Android常用组件【转】
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- C# 数字证书微信API调用使用参考事例
X.509 v.3 证书的方法.一个比较完整的调用 微信 API的示例: private stringGetResponseResult() { string strRespons ...