poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 52463 | Accepted: 16451 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff queue<pair<int,int> > qu;
bool b[]; inline bool Check(int x);
int Bfs(int r1,int r2); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int r1,r2;
scanf("%d %d",&r1,&r2);
printf("%d\n",Bfs(r1,r2));
return ;
}
inline bool Check(int x){
return x>=&&x<=&&b[x]==;
}
int Bfs(int r1,int r2){
qu.push(make_pair(r1,));
b[r1]=;
while(!qu.empty()){
pair<int,int> tmp=qu.front();
qu.pop();
if(tmp.first==r2){
return tmp.second;
}
int x=tmp.first+;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
x=tmp.first-;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
x=tmp.first*;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
}
}
我自己底下实验了下,把STL去掉,自己实现简易的队列,再进行暴搜,时间消耗为16ms。
代码二:【0ms】bfs剪枝(这里没有用STL): 剪枝内容:当自己的位置大于k的时候,自然不用考虑+1和*2的情况了。
#include <fstream>
#include <iostream>
#include <cstdio> using namespace std; const int N=;
int n,k,dis[N],queue[N];//dis:记录走到某个点的步数。 queue:因为每次队列采用去重加入,所以N是足够用了。
bool b[N];//N是足够的,不需要扩展2N或者3N。打比说x先乘再减,与先减再乘,后者会更快,故不会超过N,负数是更不可能的,因为没有除法。 int bfs(); int main()
{
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
scanf("%d%d",&n,&k);
printf("%d",bfs());
return ;
}
int bfs(){
int l,r,x;
l=r=;
dis[n]=;
queue[]=n;
while(){//队列不可能为空
if(queue[l]==k) return dis[k];
if(x=queue[l]-,x>=&&!b[x]){//注意边界
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
if(x=queue[l]+,queue[l]<=k&&!b[x]){//这里的queue[l]<=k为剪枝
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
if(x=queue[l]<<,queue[l]<=k&&x<N&&!b[x]){//这里的queue[l]<=k为剪枝;注意边界
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
l++;
}
}
poj3278-Catch That Cow 【bfs】的更多相关文章
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ - 3278 Catch That Cow 【BFS】
题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...
- POJ 3278 Catch That Cow【BFS】
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj3278 【BFS】
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 97240 Accepted: 30519 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- HDU2717 Catch That Cow 【广搜】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
随机推荐
- PLSQL导出表结构
1:进行plsql后选怎Tools--------->Exports User Ojbects------------->选中需要导出的table,squence,view,type,fu ...
- HttpURLConnection连接超时问题
1.问题描述 这几天测试重构后的下载框架,发现在下载过程中如果网络中断或网络较差,个别应用的下载就会阻塞卡住,一直卡在 “正在下载 xx%”. 2.问题排查和定位 思考:网络差不应该报网络异常的错 ...
- Java 导入证书
在这个目录下: ${JAVA_HOME}\jre\lib\security 执行 keytool -importcert -trustcacerts -file yourcerfile.pem -al ...
- more 可翻页查看(一页一页翻动)
命令解释 我们查看数据的时候,使用前面提到的nl与cat.tac等等,都是将文件内容一次性输出到屏幕上,看起来不是很方便,那我们就可以使用这个命令,一页一页查看,前面的数据不至于看不到. 命令说明 [ ...
- Linux火焰图-centos
centos7.5mini安装 yum install -y yum-utils perf debuginfo-install -y perf #debuginfo-install下载了305MB的文 ...
- Vue基础知识之组件及组件之间的数据传递(五)
vue中的组件是自定的标签,可以扩展的原生html元素,封装可复用的代码 note: 1.在标签命中不要使用大写,标签名字必须用短横线隔开 2.模板中只能有一个根元素,不能使用并列标签. 定义组件 全 ...
- 本地yum源快速创建
1.建立挂载目录mkdir /rui 2.挂载iso到新建的/rui目录
- 温故而知新-mysql的一些语法show,describe,explain,fulltext
1 show show tables; 显示数据库的所有表 show databases; 显示所有数据库 show columns from table; 显示表的所有列 show grants f ...
- 20. orcle中统计一个字符串中某个字符的长度
例子1:统计一个字符串中“,”的个数: select lengthb(regexp_replace('[a,b,c,d,e,f]','[^,]',null)) as res from dual; 例 ...
- angular 参考文档
https://www.w3schools.com/angular/ 参考二: https://www.angular.cn/guide/reactive-forms