[USACO07OPEN]Catch That Cow
题目:洛谷P1588、HDU2717
题目大意:有一个人在点$n$,一头牛在点$k$,人每秒能从$x$移动到点$x+1$、$x-1$、$2x$,牛不会动,求最少多少秒后人能移动到牛所在的$k$。
思路:BFS。按照题意进行广搜。
注意:题目数据较大,如中途计算中的点$x$大于100000或小于1,则不放入队列中。
两处题目读入不太一样。
细节见代码。
C++ Code:
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
bool b[];
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
if(n>=k){//特判n≥k的情况
printf("%d\n",n-k);continue;
}
queue<int>q1,q2;
q1.push(n);
q2.push();
memset(b,,sizeof(b));
b[n]=;
while(!q1.empty()){
int p=q1.front();q1.pop();
int P=q2.front();q2.pop();
int l=p-;
if(l&&b[l]){
if(l==k){
printf("%d\n",P+);break;
}
b[l]=;
q1.push(l);
q2.push(P+);
}
l=p+;
if(l<=&&b[l]){
if(l==k){
printf("%d\n",P+);break;
}
b[l]=;
q1.push(l);
q2.push(P+);
}
l=p*;
if(l<=&&b[l]){
if(l==k){
printf("%d\n",P+);break;
}
b[l]=;
q1.push(l);
q2.push(P+);
}
}
}
return ;
}
[USACO07OPEN]Catch That Cow的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...
- [HDOJ2717]Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- springboot 打包下载数据
//文件打包下载 public static HttpServletResponse downLoadFiles(List<File> files, Htt ...
- 汇编-理解call,ret
; 有意思的东西,主函数调用子函数用汇编来理解 assume cs:codeseg codeseg segment start: main: call sub1 ; 调用子函数1, push IP1 ...
- node+express框架中连接使用mysql经验总结
最近在学习node.js,做了一个练手项目,使用node.js+express框架,配合mysql数据库和前端vue框架开发一个多人文档编辑系统. koa,express,node 通用方法连接MyS ...
- JavaScript模块化编程之AMD
简单的说一下AMD是"Asynchronous Module Definition"的缩写,意思就是"异步模块定义".它采用异步方式加载模块,模块的加载不影响它 ...
- PHP学习总结(8)——PHP入门篇之WAMPServer集成环境安装和配置
WampServer就是Windows Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.WampServer是一款由法国人开发的Apac ...
- BA-siemens-ppm模块调试
第一部分:现场接线 1. 拨码:朝向数字那一端为0,远离数字那一端为1,PPM的地址设定方法就是将拨码器拨为跟系统架构表一样的数字,比如一个1U32的编号为77020,那么它的编号就是20,将4和16 ...
- iis配置问题报错 -- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies
具体提示: Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. 试图加载格式不正确的程序. ...
- CF 558C(Amr and Chemistry-构造法)
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- iOS项目开发实战——制作视图的缩放动画
视图的大小应该是随时可控的.今天我们就来实现对一个View的缩放动画.该动画的实现与位移动画,透明度动画稍有不同. 详细实现例如以下: import UIKit class ScaleViewCont ...
- UVA - 11021 - Tribles 递推概率
GRAVITATION, n.“The tendency of all bodies to approach one another with a strengthproportion to the ...