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,最先 ...
随机推荐
- spark 启动job的流程分析
从WordCount開始分析 编写一个样例程序 编写一个从HDFS中读取并计算wordcount的样例程序: packageorg.apache.spark.examples importorg.ap ...
- nanosleep纳秒级延迟
//函数原型 int nanosleep(struct timespec *req, struct timespec *rem) //参数列表: // req:要求的睡眠时间 // rem:剩余的睡眠 ...
- [RxJS] Filtering operator: filter
This lesson introduces filter: an operator that allows us to let only certain events pass, while ign ...
- B2B,B2C和C2C的区别
电子商务发展至今,已经有十余年了,但是对于电子商务的概念,还没有一个统一的说法.接触电子商务的人想必脑海中肯定会产生这样的疑问:电子商务和网络购物是一码事,还是两个概念?当下盛行的淘宝,当当之类的网购 ...
- 轻量级的原型设计工具-Axure RP
1. 软件下载地址: http://www.downxia.com/downinfo/25742.html 这个版本不需要注册码,不需要安装,存绿色版. 2. 基本介绍教程: http://wenku ...
- setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
- TFS 安装与管理
整了几天TFS,把相关的一些配置与安装的要点简单记下,希望对大家有用.本篇主要是安装与配置上的内容,下一篇会介绍如何使用以及使用方面的相关心得体会. 本篇内容简要: 1. 安装部署 1.1. 流 ...
- 安卓工程修改包名后 Failed to find provider info for...问题
安卓工程修改包名后 Failed to find provider info for com.android...provider问题 原因: 1. 多处含包名文件须同时更改 2. Manifest里 ...
- windows下eclipse+hadoop2
windows下eclipse+hadoop2.4开发手册 1.解压下载的hadoop2.4,到任意盘符,例如D:\hadoop-2.4.0. 2.设置环境变量 ①新建系统变量,如下所示. ②将新建的 ...
- UISearchBar 光标不出现的问题
app支持ios7,在UINavBar 里面加入搜索框,结果光标一直出现不了. 解决办法如下: searchBar.tintColor = [UIColor blueColor];