POJ-3278 廣度優化搜索入門
阿新 • • 發佈:2018-03-14
con ret lib UC pan print () scanf color
#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 廣度優化搜索入門