5 条题解

  • 1
    @ 2025-7-19 16:52:03
    #include<bits/stdc++.h>
    const int N=2e5+10;
    using namespace std;
    int n,k;
    struct node{
    	int pos,step;
    };
    bool v[N];
    void bfs(){
    	queue<node> q;
    	q.push((node){n,0});
    	v[n]=1;
    	while(!q.empty()){
    		node top=q.front();
    		q.pop();
    		if(top.pos==k){
    			cout<<top.step;
    			exit(0);
    		}
    		if(top.pos+1<=k&&!v[top.pos+1]){
    			v[top.pos+1]=1;
    			q.push((node){top.pos+1,top.step+1});
    		}
    		if(top.pos-1>=0&&!v[top.pos-1]){
    			v[top.pos-1]=1;
    			q.push((node){top.pos-1,top.step+1});
    		}
    		if(top.pos*2<N&&!v[top.pos*2]){
    			v[top.pos*2]=1;
    			q.push((node){top.pos*2,top.step+1});
    		}
    	}
    }
    int main(){
    	cin>>n>>k;
    	bfs();
    }
    
    

    信息

    ID
    1345
    时间
    1500ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    593
    已通过
    149
    上传者