hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7909 Accepted Submission(s):
2498
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.
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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; int dir[]= {,-}; struct node
{
int x,step;
} s,ss; int bfs(int n,int k)
{
queue<node>q,qq;
s.x=n;
s.step=;
int vis[]= {};
q.push(s);
while (!q.empty())
{
s=q.front();
q.pop();
if (s.x==k)
return s.step;
for (int i=; i<; i++)
{
ss.x=s.x+dir[i];
ss.step=s.step+;
if (ss.x>=&&ss.x<=)
if (!vis[ss.x])
{
vis[ss.x]=;
q.push(ss);
}
}
ss.x=s.x*;
ss.step=s.step+;
if (ss.x>=&&ss.x<=)
{
if (!vis[ss.x])
{
vis[ss.x]=;
q.push(ss);
}
}
}
return ;
} int main ()
{
int n,k;
while (~scanf("%d%d",&n,&k))
{
printf ("%d\n",bfs(n,k));
}
return ;
}
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,最先 ...
- 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 ...
- 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)
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
简单的广搜: #include <cstdio> #include <queue> using namespace std; ],step[]; int n,start,end ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 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)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- Jrebel 工具学习
Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率.网上可搜索到破解版. http://baike.baidu.com/link?url=wuzv7Wa7SMUKltJr-dyta ...
- (转)linux IO 内核参数调优 之 参数调节和场景分析
1. pdflush刷新脏数据条件 (linux IO 内核参数调优 之 原理和参数介绍)上一章节讲述了IO内核调优介个重要参数参数. 总结可知cached中的脏数据满足如下几个条件中一个或者多个的时 ...
- c++的一些编程技巧和细节
1.函数形参,如: CreateProcess( NULL, cmdbuf, NULL, ...
- MySQL中的条件语句
判断学生表中成绩是否小于60,将小于60的学生成绩列为不及格 学生表(student) 字段:姓名(name),学号(主键)(num),性别(sex),成绩(score) select *,if(sc ...
- 【python】使用枚举类
当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单,缺点是类型是int,并且仍 ...
- SP263 PERIOD - Period
题目描述 For each prefix of a given string S with N characters (each character has an ASCII code between ...
- P1483 序列变换
题目描述 给定一个由n个整数构成的序列,你需要对它进行如下操作: 操作1:输入格式“1 x y”,表示把所有a[kx](k为正整数,kx<=n)都加上y. 操作2:输入格式“2 j”,表示输出a ...
- hihoCoder#1838 : 鎕鎕鎕 贪心
---题面--- 题解: 神奇的贪心题,,,感觉每次做贪心题都无从下手... 我们首先按照a对所有卡片从小到大排序,然后从1开始,从连续的两张牌中取b最大的,最后一张单出来的也取了. 可以证明,这样的 ...
- BZOJ3894:文理分科——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3894 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理 ...
- IP协议简介
一.IP 1.IP是TCP/IP协议簇中最为核心的协议,所有的TCP.UDP.ICMP及IGMP数据都是以IP数据报格式传输. 2.IP提供不可靠.无连接的数据报传送服务 (1)不可靠:不保证IP数据 ...