样例过了但是9W+1T,求调
查看原帖
样例过了但是9W+1T,求调
706760
xiaofeng_and_xiaoyu楼主2023/7/2 19:34

rt

#include<bits/stdc++.h>
using namespace std;
queue<int> l;
queue<int> q;
bool r[1000000];
int main(){
	int t;
	cin>>t;
	while(t--){
		memset(r,0,sizeof(r));
		int a,b;
		cin>>a>>b;
		l.push(a);
		q.push(0);
		while(l.front()!=b){
			if(l.front()<b){
				if(!r[l.front()+1]){
					l.push(l.front()+1);
					q.push(q.front()+1);
					r[l.front()+1]=1;
				}
				if(!r[l.front()*2]){
					l.push(l.front()*2);
					q.push(q.front()+1);
					r[l.front()*2]=1;
				}
			}
			if(!r[l.front()]-1){
				l.push(l.front()-1);
				q.push(q.front()+1);
				r[l.front()-1]=1;
			}
			l.pop();
			q.pop();
		}
		cout<<q.front()<<endl;
		while(!q.empty()){
			l.pop();
			q.pop();
		}
	}
	
	return 0; 
}
2023/7/2 19:34
加载中...