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?
InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input
5 17
Sample Output
4
Hint
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<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int sum;
int ok=;
struct Node
{
int x;
int y; };
int book[]; void dfs(int n,int m)
{
memset(book,,sizeof(book));
queue<Node >s;
book[n]=;
Node t;
t.x=n;t.y=;
s.push(t);
int a;
Node tt;
while(!s.empty())
{
a=s.front().x*;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
a=s.front().x+;
tt.x=a;
tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); };
a=s.front().x-;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
s.pop(); }
return ; }
int main()
{ int n,m;
while(cin>>n>>m)
{
sum=;
if(n>=m) sum=n-m;
else
dfs(n,m);
cout<<sum<<endl; }
return ; }
Catch That Cow(广搜)的更多相关文章
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Catch That Cow(BFS广搜)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- HDU2717 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 (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- 广搜 poj3278 poj1426 poj3126
Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...
随机推荐
- Loadrunner之脚本篇——事务函数
1.事务的开始和结束名称需要相同 lr_start_transaction(“transaction_name”); …//事务处理 lr_end_transaction(“transaction_n ...
- Python编程-绑定方法、软件开发
一.绑定方法与非绑定方法 1.绑定方法 绑定给谁,谁来调用就自动将它本身当作第一个参数传入 (1)绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_method( ...
- POJO、Bean和JavaBean
本文总结自: https://blog.csdn.net/chenchunlin526/article/details/69939337 POJO (plain pld java object) 一个 ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第5周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 第七章:内部类与异常类 内部类(nested classes),面向对象程序设计中, ...
- Vue.js学习笔记 第八篇 组件
全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- JSP--内置对象&动作标签介绍
1.JSP中常用的9大内置对象? 内置对象:在JSP页面中能直接使用的对象就是JSP内置对象,事实上,JSP底层就是一个java类,可以在JSP中直接使用的,必然存在JSP翻译后的java类 下面简单 ...
- window.open、window.showModalDialog和window.showModelessDialog 的区别[转]
一.前言 要打开一个可以载入页面的子窗口有三种方法,分别是window.open.window.showModalDialog和window.showModelessDialog. open方法就是打 ...
- SQL中的5种常用的聚集函数
首先你要知道 where->group by->having->order by/limit ,这个就是写sql语句时的顺序 常用的5个聚集函数: Max ...
- [转载]Spring配置文件详解一:
原文地址:与base-package="com.xx">Spring配置文件详解一:<context:annotation-config/>与<contex ...
- webview 最简单的demo
) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...