答案对但是TLE,玄关
查看原帖
答案对但是TLE,玄关
989143
Nahida_Official楼主2024/11/5 20:18
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,step;
	node(int a,int b){
		x=a,step=b;
	}
};
int n,k;
queue<node> que;
bool mark[100010];
int main(){ 
	cin>>n>>k;
	if(n==k){
		cout<<0;
		return 0;
	} 
	que.push(node(n,0));
	mark[n]=1;
	while(!que.empty()){
		node head=que.front();
		que.pop();
		int cur=head.x;
		for(int i=0;i<3;i++){
			int nxt;
			if(i==0)
				nxt=cur-1;
			else if(i==1)
				nxt=cur+1;
			else
				nxt=2*cur;
			if(nxt>=0 && nxt<=100000 && mark[nxt]==0) {
				que.push(node(nxt,head.step+1));
				mark[nxt]=1;
				if(nxt==k){
					cout<<head.step+1;
					return 0;
				} 
			}
		} 
	}
	return 0;
}
2024/11/5 20:18
加载中...