bzoj1646[Usaco2007 Open]Catch That Cow 抓住那只牛*

bzoj1646[Usaco2007 Open]Catch That Cow 抓住那只牛

题意:

数轴上,起点在n,终点在k,每次走可以向左走一步或向右走一步或瞬移到当前坐标的两倍位置,问最少走几次。0≤n,k≤100000。

题解:

bfs,允许走的位置边界为[0,max(n,k)+1]。下界为0原因是如果走到小于0的位置,k≥0,则瞬移和往左走都是南辕北辙,只能向右走,那么一开始就不应该走到小于0的位置导致浪费时间。上界为max(n,k)+1的原因是如果你走到了大于这个数的位置,k必定小于当前位置,则你必须一步一步的往回走,而这样做显然比之前就别走到这个位置花更多时间……(我承认我乱扯一通)所以复杂度为100000。

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <queue>
 5 #define maxn 100100
 6 #define inc(i,j,k) for(int i=j;i<=k;i++)
 7 using namespace std;
 8
 9 queue <int> q; int tim[maxn],n,k,mx;
10 int main(){
11     scanf("%d%d",&n,&k); memset(tim,-1,sizeof(tim)); q.push(n); tim[n]=0; mx=max(n,k)+1;
12     while(!q.empty()){
13         int x=q.front(); q.pop();
14         if(x+1<=mx&&tim[x+1]==-1){q.push(x+1); tim[x+1]=tim[x]+1; if(x+1==k)break;}
15         if(x-1>=0&&tim[x-1]==-1){q.push(x-1); tim[x-1]=tim[x]+1; if(x-1==k)break;}
16         if(x<<1<=mx&&tim[x<<1]==-1){q.push(x<<1),tim[x<<1]=tim[x]+1; if(x<<1==k)break;}
17     }
18     printf("%d",tim[k]); return 0;
19 }

20160802

时间: 2024-08-01 20:06:38

bzoj1646[Usaco2007 Open]Catch That Cow 抓住那只牛*的相关文章

BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )

BFS... -------------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #define rep( i , n ) for( int i = 0

Catch That Cow抓住那只奶牛(BFS,广度优先搜索)

# **Catch That Cow(抓住那只奶牛)**[题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个过程是线性的)具体的题目请见:[原题链接:](http://poj.org/problem?id=3278).思路: 从题目中位置移动就可以看出是一个搜索问题,又加上要找最快的方法,也就是在暗示这个是一个广搜题,每次搜索有三个方向,是一个常规题,不过也有细节,请跟随笔者我思路. 代码: #includ

bzoj1646:抓住那只牛

1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 829  Solved: 393[Submit][Status][Discuss] Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He star

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

hdoj 2717 Catch That Cow 【bfs】

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 88   Accepted Submission(s) : 36 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to c

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 10

Catch That Cow

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 113   Accepted Submission(s) : 46 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to