现在的位置: 首页 > 综合 > 正文

hdu 1548 A strange lift

2018年12月28日 ⁄ 综合 ⁄ 共 615字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<queue>
using namespace std;
#define N 202
struct point
{
    int x,step;
};
int f[N],st,en,n,a[N];
int bfs()
{
    memset(f,0,sizeof(f));
    queue<point>q;
    point cur,next;
    int x,y;
    cur.step=0;cur.x=st;
    f[st]=1;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur.x==en)
            return cur.step;
            x=cur.x+a[cur.x];
            y=cur.x-a[cur.x];
            if(x>0&&x<=n&&f[x]==0)
            {
                next.x=x;next.step=cur.step+1;
                f[x]=1;
                q.push(next);
            }
            if(y>0&&y<=n&&f[y]==0)
            {
               next.x=y;next.step=cur.step+1;
                f[x]=1;
                q.push(next);
            }
    }
    return -1;
}
int main()
{
   int i,temp;
   while(scanf("%d",&n),n)
   {
       scanf("%d%d",&st,&en);
       for(i=1;i<=n;i++)
           scanf("%d",&a[i]);
       temp=bfs();
       printf("%d\n",temp);
   }
return 0;
}

抱歉!评论已关闭.