POJ-3278 广度优化搜索入门

#include<stdio.h>
#include<stdlib.h>
struct node{
    int x;
    int s;
}s[400005];
int main(){
    int n,m,book[400005]={0};
        scanf("%d %d",&n,&m);
        if(n==m)
            printf("0\n");
        else{
            int tail=1,head=1;
                s[tail].x=n;
                s[tail++].s=0;
                book[n]=1;
            int next[3]={1,-1,2};
            int flag=0;
            while(head<tail){
                for(int i=0;i<3;i++){
                    int dx;
                    if(i!=2)
                        dx=s[head].x+next[i];
                    else
                        dx=s[head].x*next[i];
                    if(dx<0||dx>400005)
                        continue;
                        if(book[dx]==0){
                            book[dx]=1;
                            s[tail].x=dx;
                            s[tail++].s=s[head].s+1;
                        }
                        if(dx==m){
                            flag=1;
                            break;
                        }
                }
                    head++;
                    if(flag)
                    break;
            }
            printf("%d\n",s[tail-1].s);
        }
return 0;
}

原文地址:https://www.cnblogs.com/wlgclining/p/8566968.html

时间: 2024-10-16 05:12:49

POJ-3278 广度优化搜索入门的相关文章

倒油问题,广度优化搜索,java

有一位厨师要从盛12斤油(a桶)的桶中倒出6斤油来,可是手边只有盛8斤油(b桶)和盛5斤油(c桶)的两个桶,问如何操作才能将6斤取出来呢? class DumpOilBFS: import cn.hncu.sreach.putOil.common.Bucket; import cn.hncu.sreach.putOil.common.DumpCase; import cn.hncu.sreach.putOil.common.MySet; /* 有一位厨师要从盛12斤油(a桶)的桶中倒出6斤油来,

POJ 1579 Function Run Fun 【记忆化搜索入门】

题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20560   Accepted: 10325 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <=

搜索入门之dfs--经典的迷宫问题解析

今天来谈一下dfs的入门,以前看到的dfs入门,那真的是入门吗,都是把dfs的实现步骤往那一贴,看完是知道dfs的步骤了,但是对于代码实现还是没有概念.今天准备写点自己的心得,真的是字面意思--入门. DFS,即深度优先搜索,是一种每次搜索都向尽可能深的地方去搜索,到达尽头时再回溯进行其他结点去搜索的搜索策略.形象的说,这是一种“不撞南墙不回头”的策略. 其实也就是遍历,只不过不像一个线性数组的遍历那么直观罢了.说到回溯,每次看到这个词我都得思考一会,到底是怎么用栈进行回溯的呢?今天实际操作了一

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

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(广搜)

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

Remmarguts&#39; Date POJ - 2449 (A*搜索|k短路)

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their ne

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as