1209:Catch That Cow(bfs)
题意:
从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st。每移动一步时间是一秒。
给出两个坐标,求得从第一坐标到第二座标的最短时间。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100005;
int step[maxn];
int st,ed;
void bfs(){
int key,t;
queue<int>que;
que.push(st);
while(!que.empty()){
key=que.front();
que.pop();
if(key==ed)
break;
t=key+1;
if(t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
t=key-1;
if(0<=t&&t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
t=key*2;
if(t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
}
}
int main ()
{
scanf("%d%d",&st,&ed);
memset(step,0,sizeof(step));
bfs();
printf("%d\n",step[ed]);
return 0;
}
1209:Catch That Cow(bfs)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- 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+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
随机推荐
- 好玩的获取目录信息的例子[C#]
DirectoryInfo dirinfo = new DirectoryInfo("d:\\111"); DirectoryInfo[] dirs = dirinfo.GetDi ...
- 反编译app方法
如果你没有代码,那么可以反编译该app. 这里将用到2个工具,分别是dex2jar和jd-gui.你可以在这里下载目前为止的最新版本以及示例apk. 我们以工具包里的ContactManager.ap ...
- Chapter 2 Open Book——2
It was worse because I was tired; 更糟糕的是因为我疲惫了. I still couldn't sleep with the wind echoing around t ...
- Entity Framework技巧系列之十三 - Tip 51 - 55
提示51. 怎样由任意形式的流中加载EF元数据 在提示45中我展示了怎样在运行时生成一个连接字符串,这相当漂亮. 其问题在于它依赖于元数据文件(.csdl .ssdl .msl)存在于本地磁盘上. 但 ...
- PHP之音乐ID3扩展
不知道你有没有这样的经历,一些从网上下载的MP3音乐,文件名明明是01.02这样的序号,但播放时却能显示出歌曲的正确名称来,是不是有点奇妙? 这其实都是ID3在暗中相助 ID3,一般是位于一个mp3文 ...
- 百度,google的地理编码
1.百度的地理编码:(不支持中国以外的其它城市) http://api.map.baidu.com/geocoder/v2/?ak=E974997f80db18330f8f5c61d084a677&a ...
- 12C cdb/pdb 配置监听
. PDB is not an instance, so using SID in the connection string will not work. When the database is ...
- 【dp】 比较经典的dp poj 1160
转自http://blog.sina.com.cn/s/blog_5dd8fece0100rq7d.html [题目大意]:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择 ...
- JSP中文编码问题
这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码. <%@ page language="java" pageEncoding="UTF- ...
- UMeng崩溃日志如何进行symbiolicate
Application received signal SIGSEGV (null) ( 0 CoreFoundation 0x2f2dde9b + 154 1 libobjc.A.dylib 0x3 ...