zoukankan      html  css  js  c++  java
  • hdu 1548 A strange lift(bfs||dijkstra)

    hdu 1548 A strange lift

    http://acm.hdu.edu.cn/showproblem.php?pid=1548


     

    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 is on floor A,and you want to go to floor B,how many times at least he havt 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
     

    Recommend
    8600

    广搜bfs代码:

    #include<iostream>
    #include<queue>
    using namespace std;
    int k[210];
    bool mark[210];
    struct nod
    {
     int x,step;
    }d,p;
    int bfs(int a,int b,int n)
    {
     queue<nod> q;
     mark[a]=1;
     d.x=a;
     d.step=0;
     q.push(d);
     while(!q.empty())
     {
      p=q.front();
      q.pop();
      if(p.x==b)
       return 1;
      d.x=p.x-k[p.x]; 
      d.step=p.step+1;
      if(d.x>=1&&!mark[d.x])
      {
       mark[d.x]=1;
       q.push(d);
      }
      d.x=p.x+k[p.x];
      if(d.x<=n&&!mark[d.x])
      {
       mark[d.x]=1;
       q.push(d);
      }
     }
     return 0;
    }
    int main()
    {
     int n;
     while(~scanf("%d",&n)&&n)
     {
      int a,b;
      scanf("%d%d",&a,&b);
      int i;
      for(i=1;i<=n;i++)
       scanf("%d",&k[i]);
      memset(mark,0,sizeof(mark));
      printf("%d\n",bfs(a,b,n)?p.step:-1);
     }
     return 0;
    }

    最短路dijkstra代码:

    #include<iostream>
    using namespace std;
    #define MAX 210
    #define INF 99999
    int map[MAX][MAX];
    int dis[MAX],v[MAX];
    void dijkstra(int a,int n)
    {
     memset(v,0,sizeof(v));
     int i,j,k,min;
     for(i=1;i<=n;i++)
      dis[i]=map[a][i];
     v[a]=1;
     dis[a]=0;
     for(i=2;i<=n;i++)
     {
      min=INF;
      k=a;
      for(j=1;j<=n;j++)
       if(!v[j]&&dis[j]<min)
       {
        k=j;
        min=dis[j];
       }
      v[k]=1;
      for(j=1;j<=n;j++)
       if(!v[j]&&map[k][j]<INF)
       {
        if(dis[j]>dis[k]+map[k][j])
         dis[j]=dis[k]+map[k][j];
       }
     }
    }
    int main()
    {
     int n;
     while(~scanf("%d",&n)&&n)
     {
      int a,b,k[MAX];
      scanf("%d%d",&a,&b);
      int i,j;
      for(i=1;i<=n;i++)
       for(j=1;j<=n;j++)
        map[i][j]=INF;
      for(i=1;i<=n;i++)
      {
       scanf("%d",&k[i]);
       if(i-k[i]>=1)
        map[i][i-k[i]]=1;
       if(i+k[i]<=n)
        map[i][i+k[i]]=1;
      }
      dijkstra(a,n);
      printf("%d\n",dis[b]==INF?-1:dis[b]);
     }
     return 0;
    }

  • 相关阅读:
    【模板整合计划】高阶数据结构
    【模板整合计划】高阶数据结构—线段树
    主席树【权值线段树】(转)
    Flask系列(二) 模板 templates
    用java实现Shazam 译文
    [转] 研究云计算与海量数据处理方向建议看的论文列表
    程序员应知 如何分析海量数据
    大数据技术大会
    android监控网络状态
    HP(惠普)大中华区总裁孙振耀退休感言
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999471.html
Copyright © 2011-2022 走看看