poj 3282 (bFS)
Catch That Cow
RE了N次,终于过了,数组开小了= =。。顺便联系队列
#include<stdio.h>
struct point
{
int pos;
}queue[200001];
int head=0,tail=0;
bool mat[200001]={0};
int step[200001];
bool is_empty()
{
return head==tail;
}
point dequeue()
{
return queue[head++];
}
void enqueue(point p)
{
queue[tail++]=p;
}
void vis(int pos)
{
point p={pos};
mat[pos]=1;
enqueue(p);
}
int main()
{
// freopen("in.txt","r",stdin);
int n,k;
scanf("%d%d",&n,&k);
point p={n};
enqueue(p);
mat[p.pos]=1;
while(!is_empty())
{
p=dequeue();
if(p.pos==k) break;
if(p.pos+1<=100000&&!mat[p.pos+1])
{vis(p.pos+1);step[p.pos+1]=step[p.pos]+1;}
if(p.pos-1>=0&&!mat[p.pos-1])
{vis(p.pos-1);step[p.pos-1]=step[p.pos]+1;}
if(p.pos+1<=100000&&!mat[p.pos*2])
{vis(2*p.pos);step[p.pos*2]=step[p.pos]+1;}
}
printf("%d\n",step[k]);
return 0;
}