poj 3278:Catch That Cow(简单一维广搜)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 45648 | Accepted: 14310 |
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
Source
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <queue>
- using namespace std;
- bool isw[];
- struct Node{
- int x;
- int s;
- };
- bool judge(int x)
- {
- if(x< || x>)
- return true;
- if(isw[x])
- return true;
- return false;
- }
- int bfs(int sta,int end)
- {
- queue <Node> q;
- Node cur,next;
- cur.x = sta;
- cur.s = ;
- isw[cur.x] = true;
- q.push(cur);
- while(!q.empty()){
- cur = q.front();
- q.pop();
- if(cur.x==end)
- return cur.s;
- //前后一个个走
- int nx;
- nx = cur.x+;
- if(!judge(nx)){
- next.x = nx;
- next.s = cur.s + ;
- isw[next.x] = true;
- q.push(next);
- }
- nx = cur.x-;
- if(!judge(nx)){
- next.x = nx;
- next.s = cur.s + ;
- isw[next.x] = true;
- q.push(next);
- }
- //向前跳
- nx = cur.x*;
- if(!judge(nx)){
- next.x = nx;
- next.s = cur.s + ;
- isw[next.x] = true;
- q.push(next);
- }
- }
- return ;
- }
- int main()
- {
- int n,k;
- while(scanf("%d%d",&n,&k)!=EOF){
- memset(isw,,sizeof(isw));
- int step = bfs(n,k);
- printf("%d\n",step);
- }
- return ;
- }
Freecode : www.cnblogs.com/yym2013
poj 3278:Catch That Cow(简单一维广搜)的更多相关文章
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- hdu 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搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
随机推荐
- BZOJ 4423: [AMPPZ2013]Bytehattan
Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
- 关于linux中文乱码的问题。
公司让人在一台装有ubuntu14.04的机器上安装net-snmp,可是这台机器的设置很让人不喜.没关系,一个个解决它. 不能连接外网,得弄一个代理. 这个好说,在可以上外网的本机上安装squid工 ...
- livezilla账号或密码修改方法
livezilla的账号和密码不在数据库,保存在php文件里面. 今天想修改一下网站livezilla系统管理员账号和密码,去数据库找了半天没找到,推测可能是存在文件中.搜索了一下,果然是在livez ...
- maven简单配置
maven-3.3.9下载 Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周 ...
- 实现Redis的主从复制配置
实现Redis的主从复制配置比较简单,而且容易明白. 下图是要配置的主从复制结构图: 1.说明 Redis主从复制中一个主服务可以有多个从服务,一个从服务可以有多个从服务. 配置比较简单,只需要更改r ...
- 比较NHibernate和Entity Framework
葡萄牙的一位开发者 Ricardo Peres 最近发布了一篇文章,以看起来无偏见的形式对领先的两种 .NET ORM:NHibernate 和 Entity Framework 进行了比较. 我们建 ...
- centos7删除已经安装的docker
centos下可以使用yum来删除docker. 列出docker包的具体的名字. $ yum list installed | grep docker docker-engine.x86_64 -0 ...
- Java对象排序
java实现对象比较,可以实现java.lang.Comparable或java.util.Comparator接口 //Product.java import java.util.Date; //p ...
- js事件监听器用法实例详解
这篇文章主要介绍了js事件监听器用法,以实例形式较为详细的分析了javascript事件监听器使用注意事项与相关技巧,需要的朋友可以参考下本文实例讲述了js事件监听器用法.分享给大家供大家参考.具体分 ...
- 最牛逼android上的图表库MpChart(一) 介绍篇
最牛逼android上的图表库MpChart一 介绍篇 MpChart优点 MpChart是什么 MpChart支持哪些图表 MpChart效果如何 最牛逼android上的图表库MpChart(一) ...