首页 > 其他 > 详细

HDU1548(楼梯问题bfs)

时间:2015-11-27 12:58:33      阅读:284      评论:0      收藏:0      [点我收藏+]
#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;
}

 

HDU1548(楼梯问题bfs)

原文:http://www.cnblogs.com/program-ccc/p/5000156.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!