题目链接: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. arttemplate 后台返回的数据格式问题

    1. 2.JSON.parse() 方法用于将一个 JSON 字符串转换为对象. $("body").on("click","#analyze&quo ...

  2. 7. Python运算符之逻辑、成员、身份运算符及优先级

    运算符 逻辑表达式 描述 and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值. or x or y 布尔& ...

  3. 前端(十六)—— JavaScript盒子模型、JS动画、DOM、BOM

    JS盒子模型.JS动画.DOM.BOM 一.JS盒模型 1.width | height parseInt(getComputedStyle(ele, null).getPropertyValue(' ...

  4. ThinkPHP5验证码不显示的原因及解决方法

    其实很久之前刚学习tp5框架的时候就遇到了这个问题,解决完后一直没再出过问题,今天用以前的框架做新项目时又碰到了这个问题,这里记录一下 问题原因: 1.TP5本就存在这个bug 2.数据库连接不正常( ...

  5. [USACO06JAN]牛的舞会The Cow Prom

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  6. 牛客网多校训练第八场A All one Matrix

    题目链接:https://ac.nowcoder.com/acm/contest/888/A 题意:求出有多少个不被包含的全1子矩阵 解题思路:首先对列做处理,维护每个位置向上1的个数,然后我们从最后 ...

  7. Asia Hong Kong Regional Contest 2019

    A. Axis of Symmetry B. Binary Tree n 的奇偶性决定胜负. C. Constructing Ranches 路径上点权之和大于,极大值两倍,这是路径上点能拼出多边形的 ...

  8. 8、如何实现可迭代对象和迭代器对象 9、如何使用生成器函数实现可迭代对象 10、如何进行反向迭代以及如何实现反向迭代 11、如何对迭代器做切片操作 12、如何在一个for语句中迭代多个可迭代对象

    8.如何实现可迭代对象和迭代器对象 PS:注意重载Iterator方法的时候,需要和原来的方法名一样,否则创建实例时会报错 from collections import Iterator,Itera ...

  9. 监控数据库SqlServer

    监控数据库的连接数select COUNT( * ) from master.dbo.sysprocesses select COUNT( * ) from master.dbo.sysprocess ...

  10. 代码控制PrivateBinPath和ConfigurationFile的位置

    原文:代码控制PrivateBinPath和ConfigurationFile的位置 .Net的WinForm程序有的时候让人很烦的是,在执行目录下总是一大堆的DLL,配置文件,最少则是个以下,多的时 ...