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 ...
随机推荐
- java反射--方法反射的基本操作
方法的反射 1)如何获取某个方法 方法的名称和方法的参数列表才能唯一决定某个方法. 2)方法反射的操作 method.invoke(对象,参数列表). 代码实例: package com.reflec ...
- Linux中内存挂载到目录下
[日期:2012-11-14] /dev/shm是linux下的一块共享内存结构.默认大小是真实内存的一半.它用来存储进程间通讯时的一些共享数据结构.在物理内存足够时,会在内存中进行数据交换,如果 ...
- Zoie中文文档及简单解析
https://linkedin.jira.com/wiki/pages/viewpage.action?pageId=4456480 一.总体架构 Zoie是一个实时的搜索引擎系统,其需要逻辑上独立 ...
- Cocos2d-x EditBox 设置字号大小无论用问题
1.有几次设置EditBox的字体大小无论用,于是跟进了Cocos2d-x 底层代码,看了下 原来是没设置字体的原因 以下是Editbox的setFontSize方法: void CCEditBox: ...
- AngularJS实现鼠标右键事件
常规javascript鼠标右键直接在标签上加contextmenu="alert('a')"即可,现在angular通过directive来定义一个右键指令. app.direc ...
- 二叉排序树及其C代码
1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树, 或者是满足例如以下性质的二叉树 ...
- HUD 2544 最短路 迪杰斯特拉算法
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- jquery $.each 和for 怎么跳出循环(终止本次循环)
1.for循环中我们使用continue:终止本次循环计入下一个循环,使用break终止整个循环. 2.而在jquery中 $.each则对应的使用return true 和return false ...
- Django REST framework 知识点总结
一.安装DjangoREST framework #先安装Django #安装必要的包 pip install djangorestframework coreapi (1.32.0+) - Sche ...
- malloc,我误解你了
malloc用于动态申请内存,这个学过C语言的都知道.忘记了在哪本书上看到,malloc申请的内存不一定是连续,于是一直记住了.这句话有错吗?没有!但是当时只是记住了这个知识点,而没有深入的思考.直到 ...