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 ...
随机推荐
- JavaScript高级程序设计:第九章
第九章 一.使用能力检测 能力检测的目标不是识别特定的浏览器,而是识别浏览器的能力.能力检测的基本模式如下: if ( object.propertyInQuestion ) { //使用object ...
- 使用AOP 使C#代码更清晰
简介 如果你很熟悉面向方面编程(AOP),你就会知道给代码增加"切面"可以使代码更清晰并且具有可维护性.但是AOP通常都依赖于第三方类库或者硬编码的.net特性来工作.虽然这些实现 ...
- php编译错误Note that the MySQL client library is not bundled anymore!
Note that the MySQL client library is not bundled anymore! 解决方法. 1. 查看系统有没有安装mysql header find / -na ...
- stream_context_create
stream_context_create作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置.代理服务器.请求方式.头信息设置的 ...
- Hunters
Hunters Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submi ...
- Ansible10:Playbook的角色与包含【转】
当单个playbook文件越来越大的时候,我们就需要重新来组织Playbooks了.我们可以将一个大的playbook拆成若干个小的playbook文件,然后通过include的方式,在主配置文件中将 ...
- 获取IP地址bash[转载]
ipaddr=`/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d : -f2 | awk '{print $1}'`
- Qt Quick里的图形效果:阴影(Drop Shadow)
Qt Quick提供了两种阴影效果: DropShow,阴影.这个元素会根据源图像,产生一个彩色的.模糊的新图像,把这个新图像放在源图像后面,给人一种源图像从背景上凸出来的效果. InnerShado ...
- 12.04 ubuntu 进入登录界面,账号密码确定是正确的但是进不来系统。
很简单,HOME目录存储太多东西了,导致开机不了.
- 转:12C CDB and pdb with sql developer
How to install the 12c DB and use the Pluggable DB with SQL DeveloperGoal To give a path to install ...