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 ...
随机推荐
- php文件锁(转)
bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件指针.o ...
- 实现基于文件存储的Session类
自主实现Session功能的类,基于文件方式存储Session数据,测试基本通过,还比较好玩,实际应用没有意义,只不过是学习Session是如何实现的. 一般基于文件存储Session数据效率不是很高 ...
- 各大浏览器内核(Rendering Engine)
记得刚开始写网页的时候,听童鞋们说各大浏览器的内核,也是懵懵懂懂的,知一不知其二,今天特地查一下: 内核只是一个通俗的说法,其英文名称为“Layout engine”,翻译过来就是“排版引擎”,也被称 ...
- Centos 6 设置静态 IP 地址
编辑文件进行IP设置: [root@test1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 原文件内容前面加#被注释掉 下面为新加的内容 #DEV ...
- JQ插件ajaxFileUpload、php实现图片,数据同时上传
代码结构如下: 1.HTML代码,没必要解释了. <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- UML2.0统一建模语言
Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到规 ...
- 一张图看懂DNS域名解析全过程
DNS域名解析是互联网上非常重要的一项服务,上网冲浪(还有人在用这个词吗?)伴随着大量DNS服务来支撑,而对于网站运营来说,DNS域名解析的稳定可靠,意味着更多用户的喜欢,更好的SEO效果和更大的访问 ...
- iOS代码实现:创建按钮,绑定按钮事件,读取控件值
// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...
- 配置安装theano环境(非GPU版)
终于成功配置了theano环境,但由于本机没有gpu,所以配置的是非gpu版本的theano,下面将具体过程进行描述(安装成功后,有时对python的各种库进行更新时,可能会导致某个模块无法调用其他被 ...
- DBA - 我的学习
DBA - 我的学习 1. df -k 检查oracle 分区硬盘使用情况 2. 检查alert_<SID名称>.log, alert日志文件,检查是否新增错误日志 3. 检查数据文件的状 ...