poj 3278 catch that cow BFS(基础水)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 61826 | Accepted: 19329 |
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
Source
[Submit] [Go Back] [Status] [Discuss]
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
vis[]; struct node{
int x;
int dis; };
node u,v; int bfs(int start,int end){
u.x=start;
u.dis=;
queue<node>q;
vis[start]=true;
q.push(u);
while(!q.empty()){
u=q.front();
q.pop();
if(u.x==end)
return u.dis;
for(int i=;i<;i++){
if(i==)
v.x=u.x+;
else if(i==)
v.x=u.x-;
else if(i==)
v.x=u.x*;
if(!vis[v.x]&&v.x>=&&v.x<=){
vis[v.x]=true;
v.dis=u.dis+;
q.push(v);
}
} }
} int main(){
int start,end;
while(scanf("%d%d",&start,&end)!=EOF){
memset(vis,false,sizeof(vis));
int step=bfs(start,end);
printf("%d\n",step);
}
return ;
}
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搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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 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 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- java Vamei快速教程19 嵌套类
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 到现在为止,我们都是在Java文件中直接定义类.这样的类出现在包(package) ...
- iptables (1) 原理
网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能有所帮助. iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防 ...
- 关于explain
> db.imooc_2.find({x:}).explain() { "queryPlanner" : { , "namespace" : " ...
- Java Web报错:getOutputStream() has already been called for this response解决方案
今天做了个导出excel表的功能.大概代码如下: ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStrea ...
- C语言exp()函数:e的次幂函数(以e为底的x次方值)
头文件:#include <math.h> exp()用来计算以e 为底的x 次方值,即ex 值,然后将结果返回.其原型为: double exp(double x); [返回值]返 ...
- 题解 P1319 【压缩技术】
这题是红题,我都觉得我的题解过不了 这道题输入不像别的题,给一个参数 n ,然后输入 n 的倍数个数据,它是给一个 n , 再输入一堆数.看题目,n × n ? 是不是就说明了给出的数和一定,都是 n ...
- Luogu [P1334] 瑞瑞的木板(手写堆)
其实这个题完全不需要用手写堆,只需要一遍遍sort就行了…… 但是! 为了练习手写堆,还是用手写堆做了. 在做本题之前,如果你没有什么思路的话,建议先做Luogu的合并果子. 好,假设你已经做过了合并 ...
- 网络编程——TCP协议和通信
第1章 TCP通信 TCP通信同UDP通信一样,都能实现两台计算机之间的通信,通信的两端都需要创建socket对象. 区别在于,UDP中只有发送端和接收端,不区分客户端与服务器端,计算机之间可以任意地 ...
- node的webserver模板
const express = require('express'); const swig =require('swig'); const fs = require('fs'); //创建服务器 c ...
- pymysql模块操作数据库及连接报错解决方法
import pymysql sql = "select host,user,password from user" #想要执行的MySQL语句 #sql = 'create da ...


