题目:洛谷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的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  3. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  4. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  6. 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 ...

  7. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  8. 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 ...

  9. [HDOJ2717]Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 用基于WebGL的BabylonJS来共享你的3D扫描模型

    转自:http://www.geekfan.net/6578/ 用基于WebGL的BabylonJS来共享你的3D扫描模型 杰克祥子 2014 年 2 月 26 日 0 条评论 标签:3D扫描 , B ...

  2. python第六周:面向对象编程

    面向对象编程: 世界万物,皆可分类 世界万物,对象 只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性 oop编程利用"类"和"对象"来创建各种模型来实现 ...

  3. 酒店管理系统(功能结构图、E-R图、用例图)

    功能结构图 用例图 状态图 流程图 数据字典 er图

  4. Spring管理流程部署——Activiti

    pom.xml <!-- activit jar 包 --> <dependency> <groupId>org.activiti</groupId> ...

  5. 开启WIFI

    C:\Windows\system32>netsh wlan set hostednetwork mode=allow ssid=wuyechun-wifi k ey= 承载网络模式已设置为允许 ...

  6. HDU 2874

    简单的tarjan #include <iostream> #include <cstdio> #include <cstring> #include <al ...

  7. NEFU 84

    其实同POJ 1061 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. Objective-C - 改变NSMutableArray的特定元素

    NSMutableArray改动元素, 使用-insertObject: atIndex: 和-replaceObjectAtIndex: withObject: 都能够, 即通过插入(insert) ...

  9. nyoj19(排列组合next_permutation(s.begin(),s.end()))

    题目意思: 从n个数中选择m个数,按字典序输出其排列. pid=19">http://acm.nyist.net/JudgeOnline/problem.php?pid=19 例: 输 ...

  10. Fragment状态保存

    这篇博文是对官方API Demo的FragmentRetainInstanceSupport.java的学习.路径在android-sdk-macosx/extras/android/support/ ...