POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278
从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内,
注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2e5+3;
int n,k;
int dp[maxn];
queue<int>que;
int main(){
scanf("%d%d",&n,&k);
memset(dp,0x7f,sizeof(dp));
dp[n]=0;
que.push(n);
bool fl=false;
while(!que.empty()){
int f=que.front();que.pop();
if(f==k){printf("%d\n",dp[f]);break;}
if(f-1>=0&&dp[f-1]>dp[f]+1){
que.push(f-1);
dp[f-1]=dp[f]+1;
}
if(f+1<=2*k&&dp[f+1]>dp[f]+1){
que.push(f+1);
dp[f+1]=dp[f]+1;
}
if(f<k&&dp[2*f]>dp[f]+1){
que.push(2*f);
dp[2*f]=dp[f]+1;
}
}
return 0;
}
POJ 3278 Catch That Cow bfs 难度:1的更多相关文章
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- poj 3278 Catch That Cow bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj 3278 Catch That Cow(bfs+队列)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- Qt::QWindow多窗口争抢置顶状态解决方案
有时候我们会有这种需求,自己的桌面程序需要置顶,但是程序包含了很多窗口,可能我们要求窗口1,2都在其它桌面程序之上,但是窗口1必须随时在窗口2之上. Qt提供的置顶方式是在windowsflags上增 ...
- MySQL两大存储引擎:MyISAM和InnoDB
Mysql有两大常用的存储引擎MyISAM,InnoDB,默认的形式是前者. 两者基本的差别是对事务处理.外键和行级锁的主持上,InnoDB支持事务处理.外键等高级特性,而MyISAM不支持.MyIS ...
- 无题II---hdu2236(二分,匈牙利)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2236 要求最大值与最小值的差值最小,是通过枚举边的下限和上限来完成 只需要用二分找一个区间,然后不断枚 ...
- 预训练模型与Keras.applications.models权重资源地址
什么是预训练模型 简单来说,预训练模型(pre-trained model)是前人为了解决类似问题所创造出来的模型.你在解决问题的时候,不用从零开始训练一个新模型,可以从在类似问题中训练过的模型入手. ...
- GlobeMapper生成Google瓦片
GlobeMapper真牛,生成自己的瓦片叠在GoogleMap的地图上,效果如下:
- 使用Vue-cli搭建项目与目录详解
1.介绍 vue-cli这个构建工具大大降低了webpack的使用难度,支持热重载,有webpack-dev-server的支持,相当于启动了一个请求服务器,给你搭建了一个测试环境,只关注开发就OK. ...
- C++之路
我学习C/C++也有两年了.开始是偏爱C语言和C++的语法特性强大,想用来做游戏开发.在深入学习的同时,逐渐了解到C++可以做很多事.大型项目需要用到运行效率高的C++,虽然运行效率越高,开发效率就要 ...
- iOS开发之开发者申请
一.对于真机调试,首先要在苹果网站上注册APP ID,以及购买iPhone Develop Program(iDP) 开发者授权,99美元.然后要创建证书请求CSR,创建步骤如下: 1.Mac O ...
- 从e.getMessage()为null看Java异常机制
问题:自定义异常触发了,但是自定义的提示信息RuntimeException却没有带过来. throw new RuntimeException("不允许插入报价主项和报价子项同时重复的记录 ...
- Ecplise项目转移到Android Studio,以及Genymotion模拟器介绍
一.移植android项目 今天简单分享一个从ecplise开发项目转移到Android Studio的方法,之前一直在ecplise上开发android项目,但是因为google现在主打Androi ...