HDU1548A strange lift BFS水题

没啥好说的,注意一下,走过的楼层不能再走,否则会陷入循环

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define maxn 210
int num[maxn],n,a,b;
int tot;
int step[maxn],vis[maxn];
bool bfs()
{
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(a);
    vis[a]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<=1;i++)
        {
            int v=u+(i?num[u]:-num[u]);
            if(v<0||v>n) continue;
            if(vis[v]==0)
            {
                vis[v]=1;
                q.push(v);
                step[v]=step[u]+1;
            }
            if(v==b)
            {
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        tot=0;
        memset(step,0,sizeof(step));
        scanf("%d%d",&a,&b);
        for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
        if(bfs()) printf("%d\n",step[b]);
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-08-09 21:28:01

HDU1548A strange lift BFS水题的相关文章

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

HDU1548A strange lift(搜索)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11649    Accepted Submission(s): 4419 Problem Description There is a strange lift.The lift can stop can at every floor as you want

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

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(){

hdu1240 bfs 水题

原题链接 思路:水题,直接搜 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x > 0

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

一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是最基本的广搜了 只是一个二叉树 所以先画个二叉树出来看一下广搜的顺序 每一个节点下一层的节点入队之后就把这个节点出队 对每一个节点 bfs的顺序是这样的 1.将这个节点的左节点入队(要判断左节点是否已经入队以及是否合适) 2.将这个节点的右节点入队(同样也要盘算右节点是否已经入队以及是否满足条件)

POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a c

HDU 1548 A strange lift——BFS

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <queue> 7 #define sc(x) scanf("%d",&(x)) 8 #define pf(x) printf("%d\n", x) 9 #def