【搜索】C - Catch That Cow

#include<stdio.h>
#include<string.h>
struct A{
    int state;
    int step;
}queue[100005];                           // 结构体数组用来模拟队列,数组元素包含两个数据,state代表遍历到的数值,step所经历的步数
int vis[100005];                          // 这个数组用来存储访问情况
int n, k;
int bfs( int aim);
int main(void){

    scanf("%d%d", &n, &k);
    if( n >= k) printf("%d\n", n-k);  // 如上图可知,n>=k的最优解一定是每一步都向后退
    else printf("%d\n", bfs(n));
    return 0;
}
int bfs( int aim){
    struct A temp;                    // 结构体元素用来当做循环单位
    int head, rear;                   // 队首队尾指针
    memset( vis, 0, sizeof(vis));
    vis[aim] = 1;
    head = rear = 0;
    queue[rear].state = aim;          // 起点作为第0个元素入队
    queue[rear++].step = 0;           // 此时也是第0步,然后队尾指针向后移动

    while( head < rear){              // 队空时结束循环
        temp = queue[head++];     // 队首元素出队
          // 第一种操作
            if( temp.state+1 > 0 && temp.state+1 <= 100005 && !vis[temp.state+1]){
            queue[rear].state = temp.state + 1;               // 操作后的元素入队,记录数值以及步数
            queue[rear++].step = temp.step + 1;
            vis[temp.step+1] = 1;                             // 此时标记访问
            if( temp.state + 1 == k) return temp.step + 1;    // 如果和目标相等则返回步数
        }
             // 第二种操作
                if( temp.state-1 > 0 && temp.state-1 <= 100005 && !vis[temp.state-1]){
            queue[rear].state = temp.state - 1;
            queue[rear++].step = temp.step + 1;
            vis[ temp.state-1] = 1;
            if( temp.state-1 == k) return temp.step + 1;
        }
             // 第三种操作
               if( temp.state*2 > 0 && temp.state*2 <= 100005 && !vis[temp.state*2]){
            queue[rear].state = temp.state * 2;
            queue[rear++].step = temp.step + 1;
            vis[ temp.state*2] = 1;
            if( temp.state*2 == k) return temp.step + 1;
        }
    }
    return 0;

本题使用DFS搜索对当前点进行N*2 N+1 N-1三种操作进行搜索

时间: 2024-10-05 15:58:48

【搜索】C - Catch That Cow的相关文章

[kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<vector> 5 #include<queue> 6 #include<cstring> 7 using namespace std; 8 int n, k, c, ans; 9 int num[100005]; 10 int main() 11 { 12 // freopen("in

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