题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1、-1、*2三种移动方式,问最少需要多少分钟抓住奶牛

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=100000+5;
const int dx[]={-1,1,2};
int d[maxn];
int n,k;
int bfs(){
	queue<int>q;
	memset(d,-1,sizeof(d));
	d[n]=0;
	q.push(n);
	while(!q.empty()){
		int now=q.front();
		q.pop();
		if(now==k) return d[k];
		for(int i=0;i<3;++i){
			int w;
			if(i==2) w=now*dx[i];
			else w=now+dx[i];
			if(w<0||w>maxn||d[w]!=-1) continue;
			q.push(w);
			d[w]=d[now]+1;
		}
	}
	return -1;
}
int main(){
	while(scanf("%d%d",&n,&k)!=EOF){
		printf("%d\n",bfs());
	}
	return 0;
}

如有不当之处欢迎指出!

poj 3278 简单BFS的更多相关文章

  1. poj 3414(简单bfs)

    题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...

  2. POJ 1101 简单BFS+题意

    The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...

  3. POJ 3278 经典BFS

    进一步了解了bfs; 题意:给你n,然后用+,-,*三种运算使n变成k; 漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候 ...

  4. POJ 3669 简单BFS

    标号 搜 完了-- //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...

  5. POJ3185(简单BFS,主要做测试使用)

    没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...

  6. BFS POJ 3278 Catch That Cow

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

  7. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  8. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  9. poj 3278 Catch That Cow bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

随机推荐

  1. CSS深入理解学习笔记之vertical-align

    1.vertical-align基本认识 支持的属性值: ①线类:baseline(默认),top,middle,bottom ②文本类:text-top,text-bottom ③上标下标类:sub ...

  2. jquery与js添加子元素

    例如在select中添加option JQuery做法: <select id="myselect" name="myselect"> </s ...

  3. WebRoot 与 webContent的区别

    Web项目发布到Tomcat中. 在Eclipse中生成项目是WebContent目录 而在MyEclipse中生成的项目目录名字好像叫做WebRoot,那么如果把MyEclipse的项目导入到Ecl ...

  4. Android Acitivy切换平移动画效果实现

    1.在anim目录下新建anim文件夹,新建tran_in.xml和tran_out.xml分别表示下一页切换进入,和本页切换出去. 即in表示下一页向左平移,out表示同样向左平移至消失. tran ...

  5. 05_Javascript进阶第一天

    内部私有函数 function a(){ alert('aaa'); return function b(){ alert('bbb'); } } //调用内部私有函数b,方法1 var func=a ...

  6. win10预览版无开始菜单解决方案

    1.按下Win+R键打开“运行”程序,键入gpedit.msc 回车以打开本地组策略编辑器 2.调到图示位置将windows设置->安全设置->本地策略->安全选项->“用户账 ...

  7. 【转】CentOS 6.3(x86_32)下安装Oracle 10g R2

    一.硬件要求 1.内存 & swap Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more 检查内存情况 # grep MemTotal / ...

  8. Codeforce A. Fair Game

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. tomcat集群与负载均衡

    参考文章http://kalogen.iteye.com/blog/784908,加上了自己调试过程中遇到的问题. 注1:实现此集群的方法参考了网上的很多文章,但由于很多文章都表明是原创的,故无法知道 ...

  10. LeetCode - 626. Exchange Seats

    Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...