HDU1548(楼梯问题bfs)

复制代码
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int MAXN=205;
typedef pair<int,int> P;
int N,A,B;
int K[MAXN];
int vis[MAXN];
int bfs()
{
    queue<P> que;
    que.push(P(0,A));
    vis[A]=1;
    while(!que.empty())
    {
        P now=que.front();que.pop();
        if(now.second==B)
        {
            return now.first;
        }
        for(int i=-1;i<=1;i++)
        {
            int next=now.second+K[now.second]*i;
            if(0<next&&next<=N&&!vis[next])
            {
                vis[next]=1;
                que.push(P(now.first+1,next));
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d",&N)!=EOF&&N!=0)
    {
        scanf("%d %d",&A,&B);
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&K[i]);
        }    
        memset(vis,0,sizeof(vis));
        printf("%d\n",bfs());
    }
    return 0;
}
复制代码

 

posted on   vCoders  阅读(418)  评论(0编辑  收藏  举报

努力加载评论中...
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示