码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1548(BFS)

时间:2015-03-08 23:02:56      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

水题,但是眼花了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;
}


hdu1548(BFS)

标签:

原文地址:http://blog.csdn.net/u013445530/article/details/44139525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!