2 条题解

  • 4
    @ 2022-10-2 15:14:17

    结构体方法

    #include<bits/stdc++.h>
    using namespace std;
    struct node{
    	int x, step;
    };
    //分身结构体 node: x表示在数轴上的位置 step表示当前分身的步数 
    queue<node>q;
    bool vis[200005];
    int main(){
    	int n, m;
    	cin>>n>>m;
    	node now;
    	now.x = n;
    	now.step = 0;
    	vis[n] = true;
    	q.push(now);
    	while(!q.empty()){
    		now = q.front();
    		q.pop();
    		if(now.x == m){
    			//如果到达目的地,输出解 
    			cout<<now.step<<endl;
    			return 0;
    		}
    		node next;
    		next.x = now.x-1;
    		next.step = now.step+1;
    		if(next.x >= 0 && !vis[next.x]){
    			//分身到x-1位置  
    			q.push(next);
    			vis[next.x] = true;
    		} 
    		next.x = now.x+1;
    		if(next.x <= m && !vis[next.x]){
    			//分身到x+1位置 
    			q.push(next);
    			vis[next.x] = true;
    		}
    		next.x = now.x*2;
    		if(next.x <= m*2 && !vis[next.x]){
    			//分身到x*2位置 
    			q.push(next);
    			vis[next.x] = true;
    		}
    	}
    	return 0;
    }
    

    双队列方法

    queue<int>q, qa;
    //队列结构体 queue<类型> 队列(变量)名; 
    //分身队列q 队列里存储分身在数轴上的位置
    //步数队列qa 队列存储对应分身属于第几次分身 
    bool vis[200005];
    int main(){
    	int n, m;
    	cin>>n>>m;
    	q.push(n);
    	//最开始的时候,本体在n位置,开始分身
    	qa.push(0);
    	//最开始的时候,本体没有分身,属于第0次 
    	vis[n] = true;
    	//标记n位置,已经有分身或本体存在 
    	while(!q.empty()){
    		//分身队列不为空,说明还有分身或本体没有分身过 
    		int x = q.front();
    		//取出分身队列 队首分身位置,开始分身
    		int y = qa.front();
    		//取出对应分身的步数 
    		q.pop();qa.pop();
    		//把队首分身干掉
    		if(x == m){
    			//如果到达目的地,输出解 
    			cout<<y<<endl;
    			return 0;
    		}
    		if(x-1 >= 0 && !vis[x-1]){
    			//分身到x-1位置  
    			q.push(x-1);
    			qa.push(y+1);
    			vis[x-1] = true;
    		} 
    		if(x+1 <= m && !vis[x+1]){
    			//分身到x+1位置 
    			q.push(x+1);
    			qa.push(y+1);
    			vis[x+1] = true;
    		}
    		if(x*2 <= m*2 && !vis[x*2]){
    			//分身到x*2位置 
    			q.push(x*2);
    			qa.push(y+1);
    			vis[x*2] = true;
    		}
    	}
    	return 0;
    }
    
    • 1
      @ 2022-10-2 15:12:17
      #include<bits/stdc++.h>
      using namespace std;
      struct node{
      int i,s;
      }f,sh;
      queue<node> q;
      bool b[10000001];
      int x,y;
      int bfs(){
      while(!q.empty()){
      f=q.front();
      q.pop();
      if(f.i==y){
      cout<<f.s<<endl;
      return 0;
      }
      int X=f.i-1;
      if(X>0&&b[X]==0){
      b[X]=1;
      sh.i=X,sh.s=f.s+1;
      q.push(sh);
      }
      X=f.i+1;
      if(b[X]==0){
      b[X]=1;
      sh.i=X,sh.s=f.s+1;
      q.push(sh);
      }
      X=f.i*2;
      if(b[X]==0){
      b[X]=1;
      sh.i=X,sh.s=f.s+1;
      q.push(sh);
      }
      }
      return 0;
      }
      int main(){
      cin>>x>>y;
      if(x>=y){
      cout<<x-y;
      return 0;
      }
      sh.i=x,sh.s=0;
      q.push(sh);
      bfs();
      }
      
      • 1

      信息

      ID
      174
      时间
      1000ms
      内存
      256MiB
      难度
      5
      标签
      递交数
      96
      已通过
      35
      上传者