码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-3278 广度优化搜索入门

时间:2018-03-14 15:07:09      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:div   clu   body   pos   bsp   nod   tail   turn   lag   

#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;
}

 

POJ-3278 广度优化搜索入门

标签:div   clu   body   pos   bsp   nod   tail   turn   lag   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!