poj 3278 Catch That Cow (bfs搜索)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 46715 | Accepted: 14673 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number
line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
(曾经那道题目的链接),后来细致看了一下题,跟那道题还是有比較大的区别,那个题目简单就在于,它的起点是从0開始的,不能往后退;这个题目就有三种情况,向前进1,向后减1,使用传送向前进2倍的距离。開始也想不到要用bfs,看到了这个题目的分类是bfs,然后採用bfs来做。
#include <cstdio>
#include <cstring>
const int maxn=200030;
typedef struct Queue //一个结构体,保存这个点的位置,和到这个点所用的时间
{
int count,step;
}Queue;
Queue queue[maxn];//队列数组
int visit[maxn];//訪问数组
void bfs(int n,int k)
{
int front=0,rear=0;
queue[rear].step=n;//把初始位置入队
queue[rear++].count=0;
visit[n]=1;//标记訪问
while(front<rear)
{
Queue q=queue[front++];
if(q.step==k)//找到终点位置
{
printf("%d\n",q.count);
break;
}
if(q.step-1>=0 && !visit[q.step-1])//注意剪枝的条件,一定要注意能够等于0
{
visit[q.step]=1;
queue[rear].step=q.step-1;//向后寻找一位
queue[rear++].count=q.count+1;//时间+1
}
if(q.step<=k && !visit[q.step+1])//剪枝
{
visit[q.step+1]=1;
queue[rear].step=q.step+1;//向前寻找一位
queue[rear++].count=q.count+1;
}
if(q.step<=k && !visit[q.step*2])//剪枝
{
visit[q.step*2]=1;//传送的情况
queue[rear].step=q.step*2;
queue[rear++].count=q.count+1;
}
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(visit,0,sizeof(visit));
bfs(n,k);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=200030;
queue<int>q;
int Count[maxn];
bool visit[maxn];
void bfs(int n,int k)
{
q.push(n);
visit[n]=1;
Count[n]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
if(x==k)
{
printf("%d\n",Count[x]);
break;
}
if(x-1>=0 && !visit[x-1])
{
q.push(x-1);
visit[x-1]=1;
Count[x-1]=Count[x]+1;
}
if(x<=k && !visit[x+1])
{
q.push(x+1);
visit[x+1]=1;
Count[x+1]=Count[x]+1;
}
if(x<=k && !visit[2*x])
{
q.push(x*2);
visit[x*2]=1;
Count[x*2]=Count[x]+1;
}
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(visit,0,sizeof(visit));
bfs(n,k);
}
return 0;
}
看到别人的一种写法,貌似也不错,分三个方向进行搜索
#include <iostream>
#include <queue>
#define SIZE 100001 using namespace std; queue<int> x;
bool visited[SIZE];
int step[SIZE]; int bfs(int n, int k)
{
int head, next;
//起始节点入队
x.push(n);
//标记n已訪问
visited[n] = true;
//起始步数为0
step[n] = 0;
//队列非空时
while (!x.empty())
{
//取出队头
head = x.front();
//弹出队头
x.pop();
//3个方向搜索
for (int i = 0; i < 3; i++)
{
if (i == 0) next = head - 1;
else if (i == 1) next = head + 1;
else next = head * 2;
//越界就不考虑了
if (next > SIZE || next < 0) continue;
//判重
if (!visited[next])
{
//节点入队
x.push(next);
//步数+1
step[next] = step[head] + 1;
//标记节点已訪问
visited[next] = true;
}
//找到退出
if (next == k) return step[next];
}
}
} int main()
{
int n, k;
cin >> n >> k;
if (n >= k)
{
cout << n - k << endl;
}
else
{
cout << bfs(n, k) << endl;
}
return 0;
}
poj 3278 Catch That Cow (bfs搜索)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
随机推荐
- [置顶] c#验证码识别、图片二值化、分割、分类、识别
c# 验证码的识别主要分为预处理.分割.识别三个步骤 首先我从网站上下载验证码 处理结果如下: 1.图片预处理,即二值化图片 *就是将图像上的像素点的灰度值设置为0或255. 原理如下: 代码如下: ...
- C++-struct类的新特性当class用
#include <iostream> #include <iomanip> #include <string> using namespace std; stru ...
- Netty IO线程模型学习总结
Netty框架的 主要线程是IO线程.线程模型的好坏直接决定了系统的吞吐量.并发性和安全性. Netty的线程模型遵循了Reactor的基础线程模型.以下我们先一起看下该模型 Reactor线程模型 ...
- Java学习——何为JNDI
曾记得在做机房收费系统的时候就接触到了API,由于它的功能非常强大,可是自己对它却不怎么了解.所以当时是又爱又怕.现在,一路走来才明确,事实上它就是一组接口.仅仅要我们去了解它就会发现.它事实上也没想 ...
- 用SignalR做类似QQ登录的应用
原文:用SignalR做类似QQ登录的应用 首先通过NuGet下载signalr包 在工程下新建一个类,继承Hub public class DemoHub:Hub { public class Us ...
- 基于visual Studio2013解决C语言竞赛题之1068指针数组
题目 解决代码及点评 /* 68. 在主函数中输入10个不等长的字符串,用另一函数对它们排序. 然后在主函数中输出这10个已排好序的字符串,用指针数组完成. */ #inclu ...
- C语言总结之---关键字
我记得我开始学习C语言的时候,那时候还在读高中,我们老师就把C语言的关键字,全部写在黑板上,老师说我们下面的两节课的内容就是(把它给记下来) 你还记得标准C有多少个关键字吗? 第一:关键字描述 C99 ...
- Logistic Regression(逻辑回归)(一)基本原理
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 虽然叫做“回归”,但是这个算法是用来解决分类问题的.回归与分类的区 ...
- (WinForm)文件夹状态监控,最小化到托盘,开机自启动
原文 (WinForm)文件夹状态监控,最小化到托盘,开机自启动 . 文件夾監控(監測文件夾中的文件動態): //MSDN上的例子 public class Watcher { public stat ...
- C 函数 strstr 的高效实现
C函数库中有一个函数 strstr(char*, char*),它实现的是在一个原字符串中查找一个子串.假设找到这种一个子串,返回这个子串在原字符串中的起始位置,若没有找到这种一个子串.则 ...