hdoj 2717 Catch That Cow
* 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.
讲解:一农夫追牛,牛很笨,他就在原地等着农夫来抓他,并且这是一条直线,农夫很容易就能找到它的,然而农夫却只有三种选择,退一步,走一步,或者走到当前位置的2倍;于是乎我们可以用搜索来解决;
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
int map[];
struct T
{
int x,step;
};
int dfs(T now)
{queue< T >q;
T end;
q.push(now);
while(!q.empty())
{
end=q.front();
q.pop();
map[end.x]=;
if(end.x==m)
{return end.step;}
//如果不是走一步一判断很容易超出内存的
now.x=end.x+;//前进一步,存入队列;
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
now.x=end.x-;//后退一步
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
now.x=end.x*;//前进2倍的位置
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
}
return ;
}
int main()
{
T now;
while(cin>>n>>m)
{
if(n>=m)//如果n大于m则只能后退了;
{
cout<<n-m<<endl;continue;
}
else
{
memset(map,,sizeof(map));
now.x=n;now.step=;
int mm=dfs(now);
cout<<mm<<endl;
}
}
return ;
}
hdoj 2717 Catch That Cow的更多相关文章
- hdoj 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)
题目链接: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
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/Oth ...
- 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 ...
- HUD 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 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)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
随机推荐
- load()和get()的区别
区别1:如果数据库中,没有userId的对象.如果通过get方法加载,则返回的是一个null:如果通过load加载,则返回一个代理对象,如果后面代码如果调用user对象的某个属性(比如user.get ...
- samba温故知新
SAMBA服务器可以实现Windows主机和Linux主机共享资源互访的功能,即在Windows下可以通过网上邻居访问Linux操作系统中SAMBA服务器共享的文件夹,当然,Linux操作系统之间同样 ...
- leetcode || 58、Length of Last Word
problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...
- 转:PHP关于反斜杠处理函数addslashes()和stripslashes()的用法
1.php处理\函数:addslashes()和stripslashes()函数 addslashes():对输入字符串中的某些预定义字符前添加反斜杠,这样处理是为了数据库查询语句等的需要.这些预定义 ...
- kubectl命令使用
语法: kubectl [command] [TYPE] [NAME] [flags] 1 command:子命令,用于操作Kubernetes集群资源对象的命令,如create, de ...
- HDUOJ----Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDUOJ----(1016)Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Sublime Text 2搭建Go开发环境(Windows)
转自:http://blog.csdn.net/love_se/article/details/7754274 下载packcontrol包地址:http://www.imjeff.cn/blog/6 ...
- JAXB注解使用[转]
一.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...