题目链接:http://poj.org/problem?id=3278

题意:有一头奶牛跑到了K的位置,农夫在N的位置,求最短抓到奶牛的时间。

农夫有两种移动方式。

1、步行:一分钟内从x->x+1 或者 x->x-1。

2、传送:一分钟内从x->2x。

题解:一个BFS例题。基础练手用的。queue里其实有三种状态。x-1,x+1,2x。然后去试探鸭。

代码:

 #include<iostream>
#include<queue>
#include<cstring>
#define Max 100010
using namespace std;
int N,K;
queue<int> qu; int vis[Max];
int step[Max]; int bfs(int n,int k){
memset(vis,,sizeof(vis));
int x;
qu.push(n);
vis[n] = ;
step[n] = ;
int head,next;
while(!qu.empty()){
head = qu.front(); //取出队首元素
if(x == k)
break; //满足条件就跳出
qu.pop(); //分方向
for(int i = ; i < ;i++){
if(i == )
next = head-;
else if(i == )
next = head+;
else if(i == )
next = head*;
if(next > Max || next < )
continue;
if( !vis[next]){
qu.push(next);
vis[next] = ;
step[next] = step[head] + ;
}
if(next == K)
return step[next];
} }
return -; } int main(){
cin>>N>>K;
if(N >= K)
cout<< N-K<<endl;
else
cout<<bfs(N,K)<<endl; return ;
}

【POJ】3278 Catch That Cow的更多相关文章

  1. 【HDOJ】2717 Catch That Cow

    bfs. /* 2717 */ #include <iostream> #include <cstdio> #include <cstring> #include ...

  2. 【搜索】C - Catch That Cow

    #include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...

  3. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  4. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  5. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  6. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  7. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

随机推荐

  1. python安装 cvxpy 巨坑,一堆C++错误

    https://www.lfd.uci.edu/~gohlke/pythonlibs/#ecos 下载scs,ecos,cvxpy的whl,一个个安装即可 之前被一堆C++错误搞晕了2小时

  2. 12、testng.xml指定运行测试包、测试类、测试方法

    目录如下: TestFixture.java 代码如下: package com.testng.cn; import org.testng.annotations.*; public class Te ...

  3. jQuery片段 - 表单action的更改和提交

    //点击表单“提交”按钮 $("#submitBut").bind("click", function() { var url = "..." ...

  4. Fedora 25技巧

    shell界面按F5插入-(波浪号,HOME) 同一个应用不同窗口切换alt + `

  5. ASP.Net 第一天笔记 MVC 控制器与视图数据传递注意事项

    1.如果方法的参数的名称与表单元素Name属性的值一致的话,会自动填充 2.如果表单元素的Name属性与实体类型中属性一致,那么表单中的数据会自动赋值给实体中的属性 3.控制器中重载的方法 方法前上边 ...

  6. Android四大组件之Service浅见

    Service 是Android四大组件之一,可以在不显示界面的情况下在后台运行.还有一个作用是通过AIDL来实现进程间通信. Service的启动方式 Service的启动方式有两种,startSe ...

  7. 上线出现[x86_64, i386]

    echo "Target architectures: $ARCHS" APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}&quo ...

  8. JAVAWEB之文件的上传和下载

    一.文件的上传: Enctype的属性介绍: 基于表单文件上传的界面简介: 文件上传时服务器端获取不到请求信息的原因及获取请求信息的几种方式: 输入流方式的实现: 实用工具包的实现:要导入fileup ...

  9. drop,delete与truncate的区别

    drop直接删掉表 truncate删除表中数据,再插入时自增长id又从1开始 delete删除表中数据,可以加where字句. (1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将 ...

  10. php 引入其他文件中的变量

    在php的开发过程中,如果所有的代码都写在同一个文件中的话,那么文件中的代码数量是否太多了,一来不便维护,二来对于编辑器也是个负担include("class0.php");在ph ...