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;
}
时间: 2024-10-12 20:33:30

hdu1548(BFS)的相关文章

HDU1548:A strange lift(Dijkstra或BFS)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:电梯每层有一个数,例如第n层有个数k, 那么这一层只能上k层或下k层,但是不能低于一层或高于n层, 给定起点与终点,要求出最少要按几次键 我的思路:这题可以用bfs或者用最短路(dijsktra) bfs AC代码 #include<cstdio>#include<iostream>#include<cstring>#include<queue>us

hdu1548 A strange lift(bfs 或Dijkstra最短路径)

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define M 205 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 9 int map[M][M]; 10 int d[M], vis[M]; 11 int n; 12 int b, e; 13 14 void Dijkstra(){

奇怪的电梯(HDU1548) (Dijkstra)或者(BFS)

问题 E: 奇怪的电梯 时间限制: 1 Sec  内存限制: 64 MB提交: 35  解决: 16[提交][状态][讨论版] 题目描述 有一天桐桐做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字K:(0≤Ki≤N).电梯只有四 个按钮:开,关,上,下.上下的层数等于当前楼层上的那个数字.当然,如果不能满足要求,相应的按钮就会失灵.例如:3 3 1 2 5代表了Ki (K1=3,K2=3,…),从一楼开始.在一楼,按“上,”可以到4楼,按“下

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; whi

hdu1548 A strange lift (简单bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15974    Accepted Submission(s): 5973 Problem Description There is a strange l

HDU1548 A strange lift BFS 简单题

一个电梯有n层,每一层有一个数k[i],和2个按钮,UP和DOWN,表示从这一层可以到达i+k[i] 或i-k[i] . 给出a,b,问从a到b 最少需要按多少下按钮. 直接bfs. 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 const int maxn=210; 6 int n,a,b; 7 int k[maxn]; 8 bool vis[maxn

hdu1548 A strange lift(bfs)

A B C D E F G C - A strange lift Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1548 Appoint description: System Crawler (2016-05-23) Description There is

HDU1548——A strange lift(最短路径:dijskstra算法)

A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "U

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};