hdu 1548 最短路||bfs

原创 2015年07月08日 09:26:28

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14882    Accepted Submission(s): 5633


Problem Description
There 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 "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
 

Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
 

Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
 

Sample Input
5 1 5 3 3 1 2 5 0
 

Sample Output
3

bfs code:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int flor[210],n,s,t,vis[210];
struct node {
int ord,ste;
};
int judge(node tem)
{
    if(tem.ord<=0 || tem.ord>n)
        return 0;
    return 1;
}
int bfs()
{
    queue<node> q;
    node tem,next;
    node a;
    a.ord=s;
    vis[s]=1;
    a.ste=0;
    q.push(a);
    while(!q.empty())
    {
        tem=q.front();
        q.pop();
        if(tem.ord==t)
            return tem.ste;
        next.ord=tem.ord+flor[tem.ord];
        if(judge(next) && !vis[next.ord])
        {
            next.ste=tem.ste+1;
            q.push(next);
            vis[next.ord]=1;
        }
        next.ord=tem.ord-flor[tem.ord];
        if(judge(next) && !vis[next.ord])
        {
            next.ste=tem.ste+1;
            q.push(next);
            vis[next.ord]=1;
        }
    }
    return -1;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int i,j;
    while(scanf("%d",&n),n)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&s,&t);
        for(i=1;i<=n;i++)
            scanf("%d",&flor[i]);
        printf("%d\n",bfs());
    }
    return 0;
}
dij code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
const int inf = 1<<30;
int map[210][210],dis[210],vis[210],n,s,t;
void dij()
{
    int i,j;
    for(i=1; i<=n; i++)
        dis[i]=map[s][i];
    dis[s]=0;
    vis[s]=1;
    int t;
    for(i=1;i<n;i++)
    {
        int minn=inf;
        for(j=1;j<=n;j++)
        {
            if(dis[j]<minn && vis[j]==0)
             {
                 t=j;
                 minn=dis[j];
             }
        }

      if(minn==inf)
                break;
        vis[t]=1;

        for(j=1;j<=n;j++)
        {
                if(dis[j]>dis[t]+map[t][j] && !vis[j])
                dis[j]=dis[t]+map[t][j];
        }
    }
}
int main()
{
   // freopen("in.txt","r",stdin);
    int i,j,ttt,tt;
    while(scanf("%d",&n),n)
    {
        scanf("%d%d",&s,&t);
        memset(dis,0,sizeof(dis));
        memset(vis,0,sizeof(vis));
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                map[i][j]=inf;
                if(i==j)
                    map[i][j]=0;
            }
        for(i=1; i<=n; i++)
        {
            scanf("%d",&ttt);
            if(i+ttt<=n)
                map[i][i+ttt]=1;
            if(i-ttt>0)
                map[i][i-ttt]=1;
        }
        dij();
        printf("%d\n",dis[t]==inf?-1:dis[t]);
    }
    return 0;
}



相关文章推荐

hdu1548A strange lift——最短路(迪杰斯特拉,spfa)。bfs(队列。数组)

Dijkstra+spfa+数组模拟队列+bfs,

HDU 1548 A strange lift(最短路问题Dijkstra & bfs两种解法)

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and t...

HDU1548 奇怪的电梯 最短路 spfa bfs

题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键。   分析:很不错的一道题。可以搜索,也可以spf...

hdu1548 最短路/BFS

指定起点和终点,用dijskal求最短路 #include #include #include #include #include #include #include #include #includ...

HDU-1548 A strange lift(最短路[Spfa || BFS])

第一眼看就知道应该要搜索,要不是放在最短路专题,还真想不到用图论做,简单题,刚好练习一下Spfa(看到Spfa时间效率不是很稳定,还是多用Dijkstra的好) BFS占用内存少,运行快,代码少,如果...

hdu 1548 A strange lift (bfs、最短路)

题意:在电梯的第i层只能上ki层或下ki层,但是不能到达低于一层或高于n层, 给定起点与终点,要求出最少要按几次键 分析:这题可以用BFS从上下两个方向搜索,也可以用最短路径 若用最短路,则应将邻接矩...

HDU 1548 最短路---A strange life

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and t...

hdu1548 A strange lift 不错的变相最短路考察

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU 1548 A strange lift(最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题面: A strange lift Time Limit: 2000/1...

HDU 1548 A strange lift 最短路变形

点击打开链接 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1548 最短路||bfs
举报原因:
原因补充:

(最多只允许输入30个字)