HDU2717:Catch That Cow(BFS 队列)

这是一道用队列实现的BFS基础搜索题,学长给我们加这道题主要是让我们联系数据结构里面的队列,话不多说看代码吧。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <stack>
#define LL long long
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int vis[100005];
struct node
{
    int x,s;
};
queue<node> q;
int bfs(int N,int K)
{
    while(q.size())
        q.pop();
    node a,t;
    mem(vis);
    a.x=N;
    a.s=0;
    q.push(a);
    vis[N]=1;
    while(q.size())
    {
        a=q.front();
        q.pop();
        if(a.x==K)
            return a.s;
        for(int i=0;i<3;i++)
        {
            if(i==0)
                t.x=a.x+1;
            else if(i==1)
                t.x=a.x-1;
            else
                t.x=a.x*2;
            if(t.x>=0&&t.x<=100000&&!vis[t.x])
            {
                t.s=a.s+1;
                q.push(t);
                vis[t.x]=1;
            }
        }
    }
}
int main()
{
    int N,K;
    scanf("%d%d",&N,&K);
    printf("%d\n",bfs(N,K));
    return 0;
}

原文地址:https://www.cnblogs.com/zznu17-091041/p/8411138.html

时间: 2024-09-30 07:40:48

HDU2717:Catch That Cow(BFS 队列)的相关文章

HDU2717 Catch That Cow ( BFS )

Catch That Cow Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow Problem 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

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搜索)

Catch That Cow 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,00

HDU 2717 Catch That Cow(bfs)

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10575    Accepted Submission(s): 3303 Problem Description Farmer John has been informed of the location of a fugitive cow and want

POJ 3275 Catch That Cow(bfs)

Catch That Cow 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

Catch That Cow BFS求线性双向最短路径

Catch That Cow 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

HDU2717 Catch That Cow 【广搜】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7147    Accepted Submission(s): 2254 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

poj 3278 catch that cow BFS(基础水)

Catch That Cow 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,00