【POJ】3278 Catch That Cow
题目链接: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的更多相关文章
- 【HDOJ】2717 Catch That Cow
bfs. /* 2717 */ #include <iostream> #include <cstdio> #include <cstring> #include ...
- 【搜索】C - Catch That Cow
#include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 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 ...
- 【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, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- JAVA JDBC大数据量导入Mysql
转自https://blog.csdn.net/q6834850/article/details/73726707?tdsourcetag=s_pctim_aiomsg 采用JDBC批处理(开启事务. ...
- vbs 之 解决打开Excel文件格式与扩展名指定格式不一致的问题
' Q:解决打开Excel文件格式与扩展名指定格式不一致的问题' A: 使用工作簿saveAs时,往往忽略掉它的第二个参数FileFormat,添加即可. 比如: set bookDiff = oEx ...
- pcre2 正则库
\S+ 不能匹配到字符串末尾的最后一个字段
- Java DOM解析器
文档对象模型是万维网联盟(W3C)的官方推荐.它定义了一个接口,使程序能够访问和更新样式,结构和XML文档的内容.支持DOM实现该接口的XML解析器. 何时使用? 在以下几种情况时,应该使用DOM解析 ...
- 注释类型 XmlType
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlType 将类或枚举类型映射到 XML 模式类型. 用法 @Xml ...
- 2-vim-打开和新建文件-01-打开/新建文件/打开定位到文件指定行
1.新建或打开文件 命令: vim 文件名 在终端中输入vi在后面跟上文件名即可. 如果文件已经存在,会直接打开文件. 如果文件不存在,会新建一个文件. 2.打开文件并定位到文件指定行. 命令: vi ...
- 45-Ubuntu-用户管理-10-chmod修改文件|目录权限
1.将a.py的权限修改为u=rwx, g=r-x, o=r--. 2.将目录test及子目录和文件权限修改为u=rwx, g=rwx, o=r-x.
- angulatJs 前端数据分页展示——例
注:css用的是amazeui html: ···<div style="height:500px;overflow: auto;"> <table class= ...
- spring_入门配置和注入
Spring的获取容器: public static void main(String[] args) { //获取核心容器 BeanFactory延迟加载对象 ApplicationContext ...
- 结对编程UI
GitHub:https://github.com/zsl1996/UI/commits/master 一. 实验内容 这是交付给最终用户的软件,有一定的界面和必要的辅助功能.完 ...