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) ...
随机推荐
- HDUOj Ignatius and the Princess III 题目1002
母函数 组合数学 #include<stdio.h> int c1[125]; int c2[125]; int main() { int n,i,j,k; while(scanf ...
- 算法笔记_211:第七届蓝桥杯软件类决赛部分真题(Java语言A组)
目录 1 阶乘位数 2 凑平方数 3 棋子换位 4 机器人塔 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 阶乘位数 阶乘位数 9的阶乘等于:362880 它的二进制表示为:10110001001 ...
- 设定StatusBar的文字成不同的颜色
设定StatusBar上的文字,该文字以StatusBar所在Form的字型设定为准,并以form的ForeColor为字的颜色,文字过长时,自动会截除这个程式的实质意义不太大,因为当文字被盖掉後需自 ...
- Java基础2-容器篇
java基础2-容器篇 1.页首请关注 思维导航大纲 1.常用容器的类型层次结构 2.理解容器的常用思维大纲 a.空间 时间 concurrentModifyException 加载因子 3.常用类源 ...
- lambda 2
# -*- coding: utf-8 -*- #python 27 #xiaodeng def action(x): return (lambda y:x+y) act=action(99) pri ...
- Eclipse常用且不易记快捷键
大小写转换:CTRL+SHIFT+X,Y 复制行:CTRL+ALT+↑,↓(部分无法使用) 查看继承关系:CTRL+T 直接查看系统源码:CTRL+SHIFT+T 查看所有快捷键:CTRL+SHIFT ...
- Q1:Valid Parentheses
Question: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine i ...
- Yum源的优先级
yum源自定义优先级,提高下载速速! 01.Install Yum Priorities Run the Yum Priorities install commandyum install yum-p ...
- iOS 封装跑马灯和轮播效果
代码地址如下:http://www.demodashi.com/demo/14075.html 功能概述和预览 功能描述:WSL_RollView 是基于UICollectionView实现的支持水平 ...
- HDU----专题训练
Problem A Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Sub ...