BFS PKU 3278

一道很简单的,典型的BFS。

代码虐我千百遍,我待代码如初恋

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 47366   Accepted: 14869

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 - 1 or + 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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Source

USACO 2007 Open Silver

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <deque>
using namespace std;
#define MAXN 100000 + 10

int vis[MAXN];
int k;
struct node{
    int num;
    int sum;
    //int vis;
}a[MAXN];

void BFS(int n){
    queue<node>q;
    node front;
    node rear;
    front.num = n;
    vis[n] = 1;
    front.sum = 0;
    q.push(front);
    int mark1;
    int mark2;
    int mark3;
    //int mark4;
    //int sum = 0;
    while(!q.empty()){
        front = q.front();
        q.pop();
        if(front.num == k){
            printf("%d\n",front.sum);
            break;
        }
        mark1 = front.num - 1;
        mark2 = front.num + 1;
        mark3 = front.num*2;
        if(mark1>=0 && vis[mark1]==0){
            rear.num = mark1;
            rear.sum = front.sum+1;
            //sum++;
            vis[mark1] = 1;
            q.push(rear);
        }
        if(mark2<=100000 && vis[mark2]==0){
            rear.num = mark2;
            //sum++;
            rear.sum = front.sum + 1;
            vis[mark2] = 1;
            q.push(rear);
        }
        if(mark3<=100000 && vis[mark3]==0){
            rear.num = mark3;
            //sum++;
            rear.sum = front.sum + 1;
            vis[mark3] = 1;
            q.push(rear);
        }
    }
}

int main(){
    int n;
    while(~scanf("%d%d",&n,&k)){
        memset(vis,0,sizeof(vis));
        //memset(a,0,sizeof(a));
        BFS(n);
    }

    return 0;
}
时间: 2024-08-01 14:14:59

BFS PKU 3278的相关文章

BFS/poj 3278 Catch That Cow

1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 const int MAXN=200010; 6 int n,k; 7 bool v[MAXN]; 8 struct point 9 { 10 int x; 11 int step; 12 }; 13 int bfs() 14 { 15 memset(v,0,sizeof(v)); 16 queue<p

BFS PKU 1915

渣渣的我做算法只能做水题啊.. 又是一道BFS水题 代码虐我千百遍,我待代码如初恋.我对代码是真爱啊 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22041   Accepted: 10294 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him ca

ppoj 3278搜索bfs+剪枝

http://poj.org/problem?id=3278 大致题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 解题思路: 说实话,要不是人家把这题归类到BFS,我怎么也想不到用广搜的= = 自卑ing... 水题水题,三入口的BFS 注意的地方有二: 1.  由于用于广搜的 队列数组 和 标记数组  相当大,如果定义这两个数组时把它们扔到局部去,编译是可以的,但肯定执行不了,提交就等RE吧= = 大数组必须开为 全局 ...常识常识.

POJ 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 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,00

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

POJ 3278 Catch That Cow(模板——BFS)

题目链接:http://poj.org/problem?id=3278 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

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10