1888. 陆行鸟挖宝

Andrew-Malcom

include

using namespace std;
const int MAX=300000;
int visited[MAX+10]={0};
struct Search{
        int x;
        int steps;
        Search(int xx,int yy):x(xx),steps(yy){}
};
int main()
{
        int a,b;
        while(cin>>a>>b){
                memset(visited,0,sizeof(visited));
                queue<Search>bird;
                bird.push(Search(a,0));
                visited[a]=1;
                while(!bird.empty()){
                        Search xxx=bird.front();
                        if(xxx.x==b){
                                cout<<xxx.steps<<endl;
                                break;
                        }
                        else{
                                if(xxx.x-1>=0&&visited[xxx.x-1]==0){
                                        visited[xxx.x-1]=1;
                                        bird.push(Search(xxx.x-1,xxx.steps+1));
                                }
                                if(xxx.x+1<=MAX&&visited[xxx.x+1]==0){
                                        visited[xxx.x+1]=1;
                                        bird.push(Search(xxx.x+1,xxx.steps+1));
                                }
                                if(xxx.x*2<=MAX&&visited[xxx.x*2]==0){
                                        visited[xxx.x*2]=1;
                                        bird.push(Search(xxx.x*2,xxx.steps+1));
                                }
                        }
                        bird.pop();
                }
        }
}
Sigurd

(๑˙ー˙๑)剪枝剪得好,DFS也能过

你当前正在回复 博客/题目
存在问题!