hdu1548(BFS)

水题,但是眼花了wa好几发。。。

#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define maxn 210
#define LL long long
const LL  INF = 100000000002;
bool visit[maxn];
int N,A,B;
typedef struct  node
{
    int f,up,down,step;
    node(int a, int b , int c ,int d)
    {
        f = a;
        up = b;
        down = c;
        step = d;
    }
    node(){};
}Floor;
Floor  fl[maxn];


int BFS(int st, int ed)
{
    memset(visit,false,sizeof(visit));
    queue<Floor> Q;
    Q.push(fl[st]);
    visit[st] = true;

    while(!Q.empty())
    {
        Floor temp = Q.front();
        Q.pop();
        if(temp.f == B) return temp.step;
        int up = temp.f + temp.up;
        int down = temp.f + temp.down;
        if(up >= 1 && up <= N  && !visit[up])
        {
            visit[up] = true;
            if(up == ed) return temp.step +1;
            else Q.push(node(up,fl[up].up,fl[up].down,temp.step+1));
        }
        if(down >= 1 && down <= N && !visit[down])
        {
            visit[down] = true;
            if(down == ed) return temp.step +1;
            else Q.push(node(down,fl[down].up,fl[down].down,temp.step+1));
        }
    }
    return -1;
}
int  main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz

    while(cin>>N && N)
    {
        cin>>A>>B;
        for(int i = 1; i <= N; i++)
        {
            cin>>fl[i].up;
            fl[i].f = i;
            fl[i].down = -1*fl[i].up;
            fl[i].step = 0;
        }
        cout<<BFS(A,B)<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值