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

  • 相关阅读:
    Dubbo与Zookeeper、SpringMVC整合和利用(负载均衡、容错)
    英语每日阅读---2、越来越多人反对人工智能参战
    新东方雅思词汇---6.3、brilli
    智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y
    英语每日写作---1、但是,人们在吹口哨时做得更好
    英语每日阅读---1、科学美国人60秒:如果觉得唱歌很难 那就吹口哨吧
    线段覆盖长度
    智课雅思词汇---二十四、形容词后缀-al-ial-ar-ary-ic-id-ish-ile-ine-oid-ory
    智课雅思词汇---二十三、动词性后缀-ate-fy-ish-ize
    iscroll.js的简单使用方法(总结)
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999471.html
Copyright © 2011-2022 走看看