pku 3278 Catch That Cow bfs

//简单的bfs,注意不要超过  133333,否则“无效内存引用”;

//x*2 - 100000 < x - 100000, 求得 x < 1333333; 为最优的数

//其实不需要,开到200000就行。


#include<stdio.h>

#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int f[250000];
struct node
{
int pos,time;
};
int t,n,k;


bool vis(int a)
{
if(a>=0 && a<=133333 && f[a]==0)
return true;
return false;
}


int bfs()
{
node sta,next;
int i,j;
sta.pos=n;
sta.time=0;
f[n]=1;
queue<node>q;
q.push(sta);
while(!q.empty())
{
sta=q.front();
q.pop();
if(sta.pos == k)
return sta.time;

for(i=1;i<=3;i++)
{
switch(i)
{
case 1:next.pos =sta.pos+1;break;
case 2:next.pos =sta.pos-1;break;
case 3:next.pos =sta.pos*2;break;
}
if(vis(next.pos))
{
next.time=sta.time+1;
f[next.pos]=1;
q.push(next);
}
}

}
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
memset(f,0,sizeof(f));
scanf("%d%d",&n,&k);
int sum=bfs();
printf("%d\n",sum);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值