hdu 2717 Catch That Cow

点击打开链接

/*

自己做的第一个广搜。。

一次AC

*/

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
int n,k,map[100001];
struct node
{
	int x,step;
};
int bfs()
{
	queue<node>q;
	node cur,next;
	cur.x=n;
	cur.step=0;
	memset(map,0,sizeof(map));
	map[n]=1;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.x==k)
			return cur.step;
		
		next.x=cur.x+1;
		if(next.x<=100000&&map[next.x]==0)
		{
			map[next.x]=1;
			next.step=cur.step+1;
			q.push(next);
		}
		next.x=cur.x-1;
		if(next.x>=0&&map[next.x]==0)
		{
			map[next.x]=1;
			next.step=cur.step+1;
			q.push(next);
		}
		next.x=2*cur.x;
		if(next.x<=100000&&map[next.x]==0)
		{
			map[next.x]=1;
			next.step=cur.step+1;
			q.push(next);
		}
	}
}
int main()
{
	int c;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		if(n==k) c=0;
		else c=bfs();
			printf("%d\n",c);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/yyf573462811/p/6365413.html