【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 ...
随机推荐
- 2. Pycharm的介绍与使用
使用Python原生IDLE IDLE是Python软件包自带的一个集成开发环境,点击开始-->Python安装包-->IDLE.启动 IDLE 时,会显示>>>,可以在 ...
- 2019杭电多校第一场hdu6579 Operation(线性基)
Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...
- ping命令的应用
Ping命令是工作在 TCP/IP网络体系结构中应用层的一个服务命令, 主要功能是向特定的目的主机发送 ICMP(Iternet Control Message Protocol 因特网报文控制协议) ...
- XVIII Open Cup named after E.V. Pankratiev. GP of SPb
contest Link A. Base i − 1 Notation solved by sdcgvhgj 238 求出a+b的2进制后从低位到高两位两位地转化为i-1进制 i-1进制的第2k位和第 ...
- ECMS清除挂马以及后台升级实战(从ecms6.6~ecms7.0)
当时状况 Windows Server 2008 R2 Enterprise + 帝国CMS6.6 + MySql server软件: Microsoft-IIS/7.5 操作系统: WINNT ...
- 网络编程之 TCP-UDP的详细介绍
一.TCP协议 1. TCP协议的特点 1.TCP是面向连接的运输层协议.这就意味着,在使用该协议之前,必须建立TCP连接.在传输数据完毕后,必须释放已经建立的TCP连接. 2.每一条TCP连接只能有 ...
- GIT 部分记录
关于版本回退 git reset HEAD^ #回退a.py这个文件的版本到上一个版本 git reset HEAD^ a.py git reset HEAD a.py 我试了一下以上2种方式 ...
- http/tcp/ip/端口
http是www服务器和本地浏览器之间传输超文本的协议. 每一台机器都有一个属于自己的ip地址,计算机也需要知道是哪个程序来接受信息,这里也就引入了端口号,可以简单地理解每一个程序都有一个唯一的端口号 ...
- Linux 系统 /var/log/journal/ 垃圾日志清理
CentOS系统中有两个日志服务,分别是传统的 rsyslog 和 systemd-journal systemd-journald是一个改进型日志管理服务,可以收集来自内核.系统早期启动阶段的日志. ...
- wpf 解决 WPF SelectionChanged事件向上传递造成重复执行不想执行的函数的问题
例如 tabcontrol里有一个tabitem tabitem里有一个combox和一个datagrid tabcontrol combox datagrid都有SelectionChanged事件 ...