HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20259 Accepted Submission(s): 5926
* 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?
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
人每走一步有三种选择:+1,-1,*2
问你最少的步数是多少?
当牧场主所在的位置大于10W的时候,就认为他越界。
因为他有可能先去到
100010的时候 ,在回来。所以再判断的时候,
越界的最大值最好为20W。这样就不会出错了。
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 100000
int vis[*max_v+];
int n,k;
struct node
{
int x,step;
};
int f(int x)//检查合法性
{
if(x<||x>=*max_v||vis[x]==)//超出范围或者用过
{
return ;
}
return ;
}
int bfs()
{
queue<node> q;
node p,next; p.x=n;
p.step=;
vis[n]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==k)
{
return p.step;
} //+1,-1,*2 三种情况都加入队列
next.x=p.x+;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x-;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x*;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
}
}
return -;
}
int main()
{
int ans;
while(cin>>n>>k)
{
memset(vis,,sizeof(vis));
ans=bfs();
cout<<ans<<endl;
}
return ;
}
HDU 2717 Catch That Cow(常规bfs)的更多相关文章
- 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 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- 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,最先 ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- 流畅的python和cookbook学习笔记(一)
1.数据结构 1.1 内置序列类型 四种序列类型: 1.容器序列:list.tuple和collections.deque 2.扁平序列:str.bytes.bytearray.memoryview和 ...
- js如何判断字符串里面是否含有某个字符串
方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") != -1 ); // tr ...
- 类型同时存在于A.dll和B.dll中的解决办法
引用B.dll后,右键引用中的B.dll属性->别名->Test using的最前面加上 extern alias Test; 使用 Test.MyClass.DoSth();
- vue中数组删除,页面没重新渲染
创建一个组件时,数据类型是数组,在删除这个数组中的数据时,数组中的数据是对的,但页面渲染的数据却不对. 举例:(不一定复现) <ul> <li v-for="(item, ...
- 1-3 Sass 语法、编译、调试
Sass 语法格式 这里说的 Sass 语法是 Sass 的最初语法格式,他是通过 tab 键控制缩进的一种语法规则,而且这种缩进要求非常严格.另外其不带有任何的分号和大括号.常常把这种格式称为 Sa ...
- Perl学习笔记(2)----正则表达式数字匹配的一个疏忽
<Perl语言入门>第15章习题第2题如下: 用 given-when 结构写一个程序,根据输入的数字,如果它能被3整除,就打印“Fizz”:如果它能被5整除,就打印“Bin”:如果它能被 ...
- MySQL数据库(5)----删除或更新已有行
有时候,会需要删除某些行,或者修改其内容.这是候便需要用到DELETE语句和UPDATE语句. 1. DELETE 语句的基本格式如下所示: DELETE FROM tbl_name WHERE wh ...
- Nginx 性能参数优化
user www www; # ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右 worker_processes 4; # 为每个进程分配cpu,上例 ...
- BEM,SASS,LESS,bootstrap:如何有效地将这些方法,工具和框架聪明地整合?
https://medium.com/@andersonorui_/bem-sass-and-bootstrap-9f89dc07d20f Bootstrap是一个“HTML,CSS和Javascri ...
- 初级游戏外挂编程详解 windows运行原理+游戏辅助编程 游戏外挂编程
详解游戏辅助编程 [目录] 1-什么是Windows API 2-Windows进程 3-Windows 的内存的运行原理 4-windows 中句柄的概念 5-Windows的变量类型 6-辅助实现 ...