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 ...
随机推荐
- == 和equal的区别?-005
1,== 和equal的区别? ==比较两个值是否相等,equal比较对对象的引用是否一致 举例: int a = 2; int b = 2; System.err.println(a == b);/ ...
- asp.net MVC4 @Html.DropDownList的使用
在MVC4中使用Razor语法,一使用就爱上他了, 一般项目都是有一些增删改查功能,表单下拉框是经常使用的,除了用原始的<select>外,还可以用@Html.DropDownList和@ ...
- Activiti5工作流笔记三
组任务 直接指定办理人 流程图如下: import java.util.HashMap; import java.util.List; import java.util.Map; import org ...
- BZOJ 1996 合唱队(DP)
考虑从最后的队形开始依次还原最初的队形. 对于当前的队形,要么选最左边的,要么选最右边的. 如果选了左边的,那么下次选择的一定是大于它的.右边的同理. 所以定义dp[mark][l][r]为区间[l, ...
- 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询
题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...
- 【bzoj3545/bzoj3551】[ONTAK2010]Peaks/加强版 Kruskal+树上倍增+Dfs序+主席树
bzoj3545 题目描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询 ...
- 用select模拟一个socket server成型版
1.你往output里面放什么,下次循环就出什么. 2. 1.服务器端:实现了收和发的分开进行 import select,socket,queue server=socket.socket() s ...
- POJ3469:Dual Core CPU——题解
http://poj.org/problem?id=3469 题目大意: 两个CPU,处理每个任务有不同的代价,有些对任务如果不在同一个CPU就会增加代价,求最小代价. ——————————————— ...
- CF549H:Degenerate Matrix ——题解
https://vjudge.net/problem/CodeForces-549H ———————————————————————— 题目大意:给一个矩阵,每个数可以加任意的数使得该矩阵为退化矩阵( ...
- pandas模块(数据分析)------dataframe
DataFrame DataFrame是一个表格型的数据结构,含有一组有序的列,是一个二维结构. DataFrame可以被看做是由Series组成的字典,并且共用一个索引. 一.生成方式 import ...