hdoj 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): 9276 Accepted Submission(s):
2907
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?
for Farmer John to catch the fugitive cow.
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 1000100
using namespace std;
int n,m;
int vis[MAX];
struct node
{
int x;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
int judge(int x)
{
if(x < 0||x > MAX||vis[x])
return 0;
return 1;
}
void bfs(int n,int k)
{
int i,j;
priority_queue<node>q;
node beg,end;
beg.x=n;
beg.step=0;
q.push(beg);
vis[n]=1;
while(!q.empty())
{
beg=q.top();
q.pop();
if(beg.x==k)
{
printf("%d\n",beg.step);
return ;
}
end.x=beg.x-1;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
end.x=beg.x+1;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
end.x=beg.x*2;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
}
}
int main() {
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,0,sizeof(vis));
bfs(n,m);
}
return 0;
}
hdoj 2717 Catch That Cow【bfs】的更多相关文章
- 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 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- 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 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- 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 ...
- Hdoj 2717.Catch That Cow 题解
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- Ubuntu常用终端快捷键
CTRL+k:删除从光标到行尾的部分 CTRL+u:删除从光标到行首的部分 ALT+d:删除从光标到当前单词结尾的部分 CTRL+w:删除从光标到当前单词开头的部分 CTRL+a:将光标移到行首 CT ...
- iOS 获取URL中的参数
- (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url { NSError *error; NSString * ...
- 读懂IL代码(三)
由于要写毕业论文的缘故,最近比较没有时间写,总是要抽出时间抽出时间.诶,这样的生活比较烦躁. 这一篇主要写委托.类.方法的IL代码,一一来说明. 委托:搞过C#的都应该清楚,委托实际上是一个类.编译器 ...
- MFC可编辑的ListCtrl
近期由于项目的要求,需要一个可以编辑的列表控件,由于MFC提供的列表控件只支持第一行可编辑,无法满足项目需求,故只能自己动手重写一个列表控件.重写列表控件的思想为:当点击列表的某行某列时,在此处创建一 ...
- ubuntu 14.04安装amd omega 驱动
驱动共分4部分: fglrx_14.501-0ubuntu1_amd64_UB_14.01.deb 52.0MB fglrx-core_14.501-0ubuntu1_amd64_UB_14 ...
- AngularJS初体验
最近突然发现,Coding.net真是一个神奇的网站.这各网站90%的请求都是通过ajax完成的.可以发现,不管你点任何链接,网页都不会刷新,点击浏览器的返回或前进按钮也是这样,打开chrome的开发 ...
- linux之GDB常用命令汇总
查看gdb的版本号 (1)rpm -q gdb 会显示是否安装gdb及版本号 (2)gdb --version也可以 breakpoint b main; b 20; 设置断点 breakpoint ...
- 如何用C程序简单演奏乐曲
如何用C程序简单演奏乐曲 首先我们要介绍一个函数: Beep(Frequency,time) 如果我们在程序中运行这个函数 Int main(void) { Beep(Frequency,time); ...
- JSP特点
建立在servlet规范功能之上的动态网页技术. JSP文件在用户第一次请求时,会被编译成servlet,然后由servlet处理用户的请求.所以JSP可以看成运行时servlet. 1).将内容的生 ...
- 【产品体验】ONE一个
第二篇博客,加油加油~~本人产品新人,学习中,希望大家多多指教! 先来两张ONE的界面图镇楼—— ONE简介: “复杂世界里,一个就够了.”这是一款轻量级的文艺阅读应用,每日更新一张图 ...