抓住那头牛

    xiaoxiao2021-03-26  29

    链接

    #include <cstdio> #include <iostream> using namespace std; int x1,x2,min1=0; struct n{ int pos; int time; }d[200000]; int f[200000]; int main() { scanf("%d%d",&x1,&x2); d[1].pos=x1; d[1].time=0; f[x1]=1; int head=0; int tail=1; while(head<=tail) { head++; int x=d[head].pos; if(d[head].pos==x2)//是否找到了牛 { min1=d[head].time; break; } else { if(f[x-1]==0&&x-1>=0&&x-1<=100000)//边界判断 { f[x-1]=1; d[++tail].pos=d[head].pos-1; d[tail].time=d[head].time+1; } if(f[x*2]==0&&x*2>=0&&x*2<=100000) { f[x*2]=1; d[++tail].pos=d[head].pos*2; d[tail].time=d[head].time+1; } if(f[x+1]==0&&x+1>=0&&x+1<=100000) { f[x+1]=1; d[++tail].pos=d[head].pos+1; d[tail].time=d[head].time+1; } } } printf("%d",min1); return 0; }

    要有标记,还要判断边界。

    转载请注明原文地址: https://ju.6miu.com/read-663922.html

    最新回复(0)