题目链接: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. Go语言TCP Socket编程

      Golang的主要 设计目标之一就是面向大规模后端服务程序,网络通信这块是服务端 程序必不可少也是至关重要的一部分.在日常应用中,我们也可以看到Go中的net以及其subdirectories下的 ...

  2. vbs 之 wscript

    https://www.jb51.net/article/20919.htm '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ...

  3. git: 使用submodule进行托管

    问题描述: 当一个prj.git项目里引用了另外一个moduleA.git项目作为其一个子模块,由于该模块未完善后续可能将继续升级,也就是需要两套git分别管理prj.git与moduleA.git, ...

  4. 剑指offer第二版面试题11:旋转数组的最小数字(JAVA版)

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  5. 一些识别CMS的经验方法总结

    今天学到了一些识别CMS的快速方法,也算是一种信息收集经验的积累,在这里要感谢一下我的同事“gakki的童养夫”对我的大力支持. 如何判断网站的CMS? robots.txt文件 robots.txt ...

  6. 高效IO之Java IO体系(一)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 个人觉得可以用“字节流操作类和字符流操作类组成了Java IO体系”来高度概括J ...

  7. 使用ProxyBroker构建代理池

    import asyncio from proxybroker import Broker async def show(proxies): while True: proxy = await pro ...

  8. 深入vue

  9. 微信小程序のwxs

    WXS(WeiXin Script)是小程序的一套脚本语言,结合 WXML,可以构建出页面的结构. wxs可以说就是为了满足能在页面中使用js存在的,在wxml页面中,只能在插值{{ }}中写简单的j ...

  10. C语言实现 冒泡排序 选择排序 希尔排序

    // 冒泡排序 // 选择排序 // 希尔排序 // 快速排序 // 递归排序 // 堆排序 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h& ...