hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6383 Accepted Submission(s): 2034
* 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.
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
bool isw[];
int step[];
void bfs(int n,int k)
{
memset(isw,,sizeof(isw));
queue <int> q;
int cur,next;
cur = n;
step[n] = ;
isw[cur] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur==k) //找到,返回结果
return ;
int i;
for(i=;i<=;i++){ //步行,或者传送
switch(i){
case :
next = cur - ;
if(isw[next]) //剪枝,走过的不能走
break;
if(next< || next>) //剪枝,越界不能再走
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur + ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] + ;
q.push(next);
isw[next] = true;
break;
case :
next = cur * ;
if(isw[next])
break;
if(next< || next>)
break;
step[next] = step[cur] +;
q.push(next);
isw[next] = true;
break;
}
}
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
bfs(n,k);
printf("%d\n",step[k]);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)的更多相关文章
- 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,最先 ...
- Catch That Cow (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- 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)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 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 ...
- Catch That Cow(广搜)
个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...
随机推荐
- 查看正在执行的sql语句
;WITH t AS( SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME(sp.dbid), [User] = nt_username, [ ...
- [Exceptions Spring 2] - Cannot create a session after the response has been committed
2016-02-23 14:06:27,416 [http-bio-8080-exec-1] DEBUG [org.springframework.beans.factory.support.Defa ...
- chrome 设置启动时打开特定一组网页
chrome 设置启动时打开特定一组网页 CreateTime--2018年4月25日08:57:00 Author:Marydon 1.使用场景 经常有一些必用的网站,每天打开chrome都要依 ...
- Bootstrap 3 Menu Generator
Bootstrap 3 Menu Generator
- linux 源码安装 Nginx
1.安装前环境准备安装make:# yum -y install gcc automake autoconf libtool make安装g++:# yum install gcc gcc-c++ 2 ...
- SQL Server2005 两台服务器上的数据库互相同步(转载)
1.1测试环境 Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server SQL SQL Server 2005 企业版 SQL Serv ...
- 从 "org.apache.hadoop.security.AccessControlException:Permission denied: user=..." 看Hadoop 的用户登陆认证
假设远程提交任务给Hadoop 可能会遇到 "org.apache.hadoop.security.AccessControlException:Permission denied: use ...
- Openstack 03 - Nova Compute
1.前言 非常早之前就開始着手写Openstack 系列的博客了,在写了总体架构和Keystone之后,准备写Nova,可是每次写到一半,自己心里就认为不踏实,由于似乎我并没有真正理解Nova,或者说 ...
- 第二节 JVM优化应用以及知识总结
在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...
- ubuntu更新出错--Could not get lock /var/lib/dpkg/lock
ubuntu在vps上安装好后,通常第一个命令是更新系统软件.然而在运行的过程中,却出现这样的错误: E: Could not get lock /var/lib/dpkg/lock - open ( ...