POJ-3278(BFS)
题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
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 分析:题目大意是在数轴上给定任意起点n、终点k,对任意的点坐标x每次有3种走法:x-1,x+1,2*x。每走一次花费时间为1minute,问从n到k最少需要花费多少时间?
该题是最短路径问题,于是可以用BFS搜索,每次往三个方向BFS,直到到达k,每走一步记录当前时间。
//simonPR
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
int n,k,vis[maxn];
struct catche{
int site,times; //记录点和所花的时间。
};
queue<catche> q;
void init();
void init(){
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
}
int bfs(int n,int k);
int bfs(int n,int k){
if(n==k) return ;
catche now,news,start;
start.site=n;
start.times=;
q.push(start); //将起点入队列。
vis[n]=;
while(!q.empty()){
int ts;
now=q.front();
q.pop();
for(int i=;i<;i++){ //分三个方向BFS。
if(i==) ts=now.site-;
else if(i==) ts=now.site+;
else ts=*now.site;
if(ts>maxn||vis[ts]==) continue; //如果已经访问过了或超出数据范围则跳过。
if(ts==k) return now.times+; //到达终点K,返回时间。
if(vis[ts]==) //更新点信息,并将新点入队列。
{
vis[ts]=;
news.site=ts;
news.times=now.times+;
q.push(news);
}
}
}
}
int main()
{
scanf("%d%d",&n,&k);
init(); //初始化。
int ans=bfs(n,k);
printf("%d\n",ans);
return ;
}
POJ-3278(BFS)的更多相关文章
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 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 ...
- 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)
题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
随机推荐
- Android中的常见时区
方法: private void printTimeZone(){ String[] ids= TimeZone.getAvailableIDs(); for (int i = 0; i < i ...
- 深入分析iSCSI协议的应用
深入分析iSCSI协议的应用 1 引言 快速增长的存储容量使得企业需要采用网络存储解决方案.目前网络存储技术采用的连接技术主要有光纤通道和TCP/IP.基于IP的网络存储能解决基于光纤通道的网络存储中 ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- bzoj1295
考虑到这道题n,m都很小,我们考虑先穷举起点i 下面我们要做的是找出移走k个障碍后,点i所能到的最大距离 我们可以把这个问题转化为判定性问题 对于一对点i,j,如果他们之间存在一条路径,障碍数(包括起 ...
- JavaScript用JQuery呼叫Server端方法
准备好Server端的方法 [System.Web.Services.WebMethod] public static string VeryUserName(string name) { strin ...
- 【转】iOS Provisioning Profile(Certificate)与Code Signing详解 -- 待看
原文网址:http://blog.sina.com.cn/s/blog_82c8198f0102vy4j.html 引言 关于开发证书配置(Certificates & Identifiers ...
- linux下jdk的卸载与安装
JDK的卸载 1.检查jdk的是否安装,显示如下表示安装: [root@localhost ~]# rpm -aq|grep java tzdata-java-2010l-1.el6.noarch j ...
- JAVA Web项目的编码过滤器
首先写一个EncodeFilter的过滤类: package com.djtu.wy.common; import java.io.IOException;import javax.servlet.F ...
- JSON AND BSON
JSON JavaScript Object Notation (JSON) is an open, human and machine-readable standard that facilita ...
- 【oracle】初学jobs
含义:job是oracle的一种对象,可以理解为定时执行的程序 目的:定时自动执行特定代码 照猫画虎--创建job 1.创建测试表JOB_TEST create table JOB_TEST(a da ...