求大佬看一下,pojWA了
查看原帖
求大佬看一下,pojWA了
600058
qiuqiuAC楼主2022/7/22 13:11
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
int line[100010];
struct node{
	int x,y;
};
int check(int x)
{
	if(x>=0&&x<100010&&line[x]==0)
		return 1;
	else
		return 0;
}
void BFS(int a,int b)
{
	queue<node>q;
	node st,nt,nt_2,nt_3;
	st.x=a;
	nt.x=0;
	line[a]=1;
	q.push(st);
	while(!q.empty())
	{
		if(line[b]!=0)
			break;
		st=q.front();
		q.pop();
			nt.x=st.x+1;
			nt_2.x=st.x-1;
			nt_3.x=st.x*2;
			nt.y=st.y+1;
			nt_2.y=st.y+1;
			nt_3.y=st.y+1;
			if(check(nt.x))
			{
				q.push(nt);
				line[nt.x]=nt.y;
			}
			if(check(nt_2.x))
			{
				q.push(nt_2);
				line[nt_2.x]=nt_2.y;
			}
			if(check(nt_3.x))
			{
				q.push(nt_3);
				line[nt_3.x]=nt_3.y;
			}	
	}
	line[a]=0;
	cout<<line[b]<<"\n";
}
int main()
{
	memset(line,0,sizeof(line));
	int n,k;
	cin>>n>>k;
	BFS(n,k);
	return 0;
}
2022/7/22 13:11
加载中...