#include<cstdio> #include<iostream> #include<string> #include<cstring> #include<cmath> #include<stdlib.h> #include<algorithm> #include<vector> #include<queue> #include<stack> using namespace std; const int maxn=100001; bool visit[maxn]; struct Status { int n,t; Status(int n,int t):n(n),t(t){} }; int bfs(int n,int k) { queue<Status>q; q.push(Status(n,0)); visit[n]=true; while(!q.empty()) { Status current =q.front(); q.pop(); if(current.n==k) { return current.t; } for(int i=0;i<3;i++) { Status next(current.n,current.t+1); if(i==0) { next.n+=1; } else if(i==1) { next.n-=1; } else { next.n*=2; } if(next.n<0||next.n>=maxn||visit[next.n]) { continue; } q.push(next); visit[next.n]=true; } } } int main() { int n,k; while(cin>>n>>k) { memset(visit,false,sizeof(visit)); cout<<bfs(n,k)<<endl; } return 0; }
1、结构体不熟
2、BFS算法不熟
3、这个hdu2717是多组输入,所以一开始错了,改成while(cin>>n>>k)就对了