POJ3279 Catch That Cow(BFS)
本文来源于:http://blog.csdn.net/svitter
意甲冠军:给你一个数字n, 一个数字k。分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x。求主人找到奶牛的时间(奶牛不移动)
题解:最基础的BFS可是脑子犯抽WA了3遍- =
注意:
1.数组范围1~1<<5
2.visit去重。(BFS最基础的)
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; bool visit[100010]; struct step
{
int x;
int t;
step(){}
step(int a, int b):x(a), t(b){}
}; inline bool judgeNum(int i)
{
if(i > 100000 || i < 0)
return false;
return true;
} int main()
{
int n, k;
queue <step> que;
step top;
int temp; while(~scanf("%d%d", &n, &k))
{
memset(visit, 0, sizeof(visit));
visit[n] = 1;
que.push(step(n, 0));
while(!que.empty())
{
top = que.front();
if(k == top.x)
{
printf("%d\n", top.t);
while(!que.empty())
{
que.pop();
}
break;
}
temp = top.x+1;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(top.x+1, top.t+1));
visit[temp] = 1;
} temp = top.x-1;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(top.x-1, top.t+1));
visit[temp]= 1;
} temp = top.x*2;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(2*top.x, top.t+1));
visit[temp] = 1;
} que.pop();
} }
return 0;
}
POJ3279 Catch That Cow(BFS)的更多相关文章
- 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 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Catch That Cow (bfs)
Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...
随机推荐
- Swift语言Auto Layout入门教程:上篇
原文:Beginning Auto Layout Tutorial in Swift: Part 1/2,译者:@TurtleFromMars 开始用自动布局约束的方式思考吧! 更新记录:该教程由Br ...
- jquery中怎么删除<ul>中的整个<li>包括节点
.$('ul li').remove(); .$('ul li').each(function(){ $(this).remove(); }); .$("ul").find(&qu ...
- 冒泡排序 JAVA版
冒泡排序 算法思想是每次从数组末端开始比较相邻俩元素,把第i小的冒泡到数组的第i个位置.i从0一直到N-1从而完成排序.当然也可以从数组开始端开始比较相邻两元素,把第i大的冒泡到第N-i个位置.I从0 ...
- WOJ 1020
#include<stdio.h> #include<stdlib.h> int main() { int n,m; int *num,*link; int i,j,t,k=0 ...
- (ZT)LoadRunner9.0成功破解方法
LoadRunner9.0软件下载地址: http://www.3atesting.com/filedown/LR9Download.exe 破解所需文件 http://download.csdn.n ...
- CodeForce 356A Knight Tournament(set应用)
Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 通过memcached来实现对tomcat集群中Session的共享策略
近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...
- 搭建php环境时解决jpeg6 make: ./libtool:命令未找到
搭建php环境时解决jpeg6 make: ./libtool:命令未找到 [root@bogon jpeg-6b]# make; make install ./libtool --mode=comp ...
- Android_多媒体_SoundPool声音池使用
1.SoundPool概述 SoundPool就相当于一个简单的集合,可以将apk中的资源或者系统中的文件加载至内存中,使用了MediaPlayer服务区解码音频文件,用SoundPool可以播一些短 ...
- PHP_SELF、 SCRIPT_NAME、 REQUEST_URI差别
$_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在使用方法上是很相似的,他们返回的都是与当前正在使用的页面地址有关 ...