POJ 3278HDU 2717 Catch That Cow(简单BFS)

    技术2024-11-06  10

     

    给定两个整数 n 和 k ,有 n+1或n-1或n*2 这3种操作,求使得 n==k 最少的操作次数其中(0≤n≤1e5) 

    const int N=1e5; int n,m,t; int i,j,k; int a[N+5]; bool vis[N+5]; int main() { //IOS; int num=0; while(sdd(n,k)==2){ queue<int> q; q.push(n); ms(vis,0); ms(a,0); while(!q.empty()){ int x=q.front(); q.pop(); if(x==k){ pd(a[x]); break; } if(x-1>=0 && !vis[x-1]){ vis[x-1]=1; q.push(x-1); a[x-1]=a[x]+1; } if(x+1<=N && !vis[x+1]){ vis[x+1]=1; q.push(x+1); a[x+1]=a[x]+1; } if(2*x<=N && !vis[x<<1]){ vis[x<<1]=1; q.push(x<<1); a[x<<1]=a[x]+1; } } } //PAUSE; return 0; }

     

    Processed: 0.027, SQL: 9