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 ...
随机推荐
- find查找大于1M小于10M的文件 $ find . -size +1M -size -10M
查找大于1M小于10M的文件$ find . -size +1M -size -10M
- C++中出现的计算机术语1
access labels(訪问标号) 类的成员能够定义为 private,这能够防止使用该类型的代码訪问该成员. 成员还能够定义为 public,这将使该整个程序中都可訪问成员. address( ...
- HTML中Id和Name的区别
源地址:http://www.cnblogs.com/laodai/articles/2244215.html 在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号 ...
- opencv如何载入内存中的图像文件
其实很简单,cv::imdecode 支持 std::vector<uchar>的,只要把char* 转 std::vector<uchar>就行了.用 std::vector ...
- All consistent reads within the same transaction read the snapshot established by the first read.
Session 1: Session 2: mysql> show variables like '%tx_isolation%'; +---------------+------------- ...
- VC调试技巧
Visual C++ 的 C 运行时刻函数库标识模板0xCD 已经分配的数据(alloCated Data)0xDD 已经释放的数据(Deleted Data)0xFD 被保护的数据 ...
- windows下Memcached 架设及java应用
1 Memcached 介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据 ...
- Android---OpenGL ES之添加动作
本文译自:http://developer.android.com/training/graphics/opengl/motion.html 在屏幕上绘制对象是OpenGL的最基本功能,你可以使用其他 ...
- KMP算法及KMP算法的应用(POJ2406)
///KMP算法#include<bits/stdc++.h> using namespace std; ]; void makeNext(const char P[],int next[ ...
- BT5 firefox Flash插件问题
今天在BT下安装了Nessus,好不容易安装好了,注册成功,本以为大功告成,但是在最后关头,却出现一个“未安装flash插件”错误,在bT下尝试着安装flahs插件,蛋碎一地,,,没能解决. 我的BT ...