openjudge之2.5搜索经典题,2971:抓住那头牛
查看原帖
openjudge之2.5搜索经典题,2971:抓住那头牛
316827
Temperature_automata楼主2020/8/23 11:59

求助,为什么全RE

#include <iostream>
using namespace std;

int n,k,ans;
int f[]={1,-1};
bool used[100005];

void bfs(int start,int end)
{
	int head=1,tail=0,q[100005],p[100005];
	tail++;
	q[tail]=start;
	p[tail]=0;
	used[start]=1;
	while(head<=tail)
	{
		int c=q[head];
		int pp=p[head];
		head++;
		for(int i=0;i<2;i++)
		{
			if(used[c+f[i]]==0)
			{
				if(c+f[i]>=0 and c+f[i]<=100005)
				{
					tail++;
					q[tail]=c+f[i];
					p[tail]=pp+1;
					used[c+f[i]]=1;
					if(c+f[i]==end)
					{
						cout<<p[tail];
						return ;
					}
				}
			}
		}
		if(used[c*2]==0)
		{
			if(c*2>=0 and c*2<=100005)
			{
				tail++;
				q[tail]=c*2;
				p[tail]=pp+1;
				if(c*2==end)
				{
					cout<<p[tail];
					return ;
				}
			}
			
		}
	}
}

int main()
{
//	freopen(".in","r",stdin);
//	freopen("","w",stdout);

	cin>>n>>k;
	
	bfs(n,k);
	 
	return 0;
}
2020/8/23 11:59
加载中...