搜索------Catch That Cow

从一个点a走到另一个点b,中间可以经过x+1,x-1,x*2,最少几步可以到达b点

-------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
char str[105][105];
int dir[8][2] = {{1,1},{-1,1},{-1,-1},{1,-1}};
int n,k;
int a = 0;
using namespace std;
struct point
{
    int n,m;
};
int oo[111111];
int check(int n)
{
    if(n>=0&&n<=100000&&(!oo[n]))
        return 1;
    return 0;
}
int dfs(int x)
{
    queue<point>Q;
    point now,next;
    now.n = x;
    now.m = 0;
    oo[x] = 1;
    Q.push(now);
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(now.n == k)
        {
            return now.m;
        }
        next.n = now.n + 1;
        if(check(next.n))
        {
            oo[next.n] = 1;
            next.m = now.m+1;
            Q.push(next);
        }
        next.n = now.n - 1;
        if(check(next.n))
        {
            oo[next.n] = 1;
            next.m = now.m + 1;
            Q.push(next);
        }
        next.n = now.n * 2;
        if(check(next.n))
        {
            oo[next.n] = 1;
            next.m = now.m + 1;
            Q.push(next);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(oo,0,sizeof(oo));
        int ans = dfs(n);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-29 10:46:41

搜索------Catch That Cow的相关文章

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

Catch That Cow抓住那只奶牛(BFS,广度优先搜索)

# **Catch That Cow(抓住那只奶牛)**[题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个过程是线性的)具体的题目请见:[原题链接:](http://poj.org/problem?id=3278).思路: 从题目中位置移动就可以看出是一个搜索问题,又加上要找最快的方法,也就是在暗示这个是一个广搜题,每次搜索有三个方向,是一个常规题,不过也有细节,请跟随笔者我思路. 代码: #includ

Catch That Cow(广度优先搜索_bfs)

 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48036   Accepted: 15057 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

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

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 3287 (基础BFS) Catch That Cow

这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节点取出放入Closed表,并记该节点为n:(4) 考察节点n是否为目标节点.若是,则得到问题的解,成功退出:(5) 若节点n不可扩展,则转第(2)步:(6) 扩展节点n,将其不在Closed表和Open表中的子节点(判重)放入Open表的尾部,并为每一个子节点设置指向父节点的指针(或记录节点的层次)

POJ 3278: Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44613   Accepted: 13946 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