求助一道站外题(BFS相关)
  • 板块学术版
  • 楼主int4096
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/8/2 21:20
  • 上次更新2023/10/27 17:18:29
查看原帖
求助一道站外题(BFS相关)
542452
int4096楼主2022/8/2 21:20

原题

上面看不到可以看这个

这好像是一道BFS的题目,结果错了第三个点(90pts)

酷似广搜写的没有问题?求助

#include<bits/stdc++.h>
using namespace std;
int n,k,que[100005][2],t,a[100005]={0};
int main(){
	cin>>n>>k;
	if (n==k){
		cout<<0; 
		return 0;
	}
	if(n>k){
		cout<<k;
		return 0;
	}
	int head=0,tail=1;
	que[1][0]=n;
	que[1][1]=0;
	a[n]=1;
	int x;
	while(head<tail){
		head++;
		x=que[head][0];
		for(int i=1;i<=3;i++){
			if(i==1)t=x+1;
			if(i==2)t=x-1;
			if(i==3)t=x*2;
			if(t>= 0&&t<=100005&&a[t]==0){
				tail++;
				que[tail][0]=t;
				a[t]=1;
				que[tail][1]=que[head][1]+1;
				if(t==k){
					cout<<que[tail][1]; 
					return 0;
				}	
			}
		}
	}
	return 0;
}

个人认为马蜂良好

2022/8/2 21:20
加载中...