zoukankan      html  css  js  c++  java
  • HDU-1548 A strange lift

     细节输入的数据要特别考虑清楚。          

                     A strange lift

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

    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
     
    #include<stdio.h>
    #include<string.h>
    #define INF 999999
    int map[205][205],mark[205];
    int n,s,e,dist[205];
    void Dijkstra(int start )  
    {  
        int i, j, k, min;  
        memset(mark,0,sizeof(mark));  
        for ( i = 1; i <= n; i++ )  
            dist[i] = map[start][i];  
        dist[start] = 0;  
        mark[start] = 1;  
         for ( i = 1; i <=n; i++ )  
        {  
                min = INF;  
            for ( j = 1; j <= n; j++ )  
                if ( dist[j] < min && !mark[j] )  
                {  
                    min = dist[j];  
                    k = j;  
                }  
                  
            if (min == INF ) break;  
                mark[k] = 1;  
            for ( j = 1;j <=n; j++ )  
                if (map[k][j]+dist[k] < dist[j] && !mark[j] )  
                    dist[j] = map[k][j] + dist[k];  
        }  
    	 if(dist[e]==INF)
    		 printf("-1
    ");
    	 else
          printf("%d
    ",dist[e]);         
    }  
    int  main()
    {
    	int a[201],i,j;
    	while(scanf("%d",&n)&&n!=0)
    	{
    		memset(map,0,sizeof(map));
    		scanf("%d%d",&s,&e);
    		for(i=1;i<=n;i++)
    			scanf("%d",&a[i]);
    		for(i=1;i<=n;i++)
    			for(j=1;j<=n;j++)
    				map[i][j]=INF;
    		   if(s==e)//输入的数据要注意。
    		   {
    			   printf("0
    ");
    			   continue;
    		   }
    		    if(s>n||e>n)
    			{
                printf("-1
    ");
                 continue;
    			}
    		for(i=1;i<=n;i++)
    		{
    			if(i+a[i]<=n)
    			{
    				if(map[i][i+a[i]]>1)
    			        map[i][i+a[i]]=1;
    			}
    			if(i-a[i]>0)
    			{
    				if(map[i][i-a[i]]>1)
    				map[i][i-a[i]]=1;
    			}
    		}
    	  Dijkstra(s);
    	}
    	return 0;
    }
    
  • 相关阅读:
    第02组 Alpha冲刺 (3/6)
    第02组 Alpha冲刺 (2/6)
    第02组 Alpha冲刺 (1/6)
    第02组(51) 需求分析报告
    第02组(51) 团队展示
    结对编程作业
    CentOS7下利用systemd机制实现tomcat开机自启动
    mysql5.7密码修改
    last命令显示出unknown用户究竟是因为啥?
    3.图形显示设备
  • 原文地址:https://www.cnblogs.com/cancangood/p/3450392.html
Copyright © 2011-2022 走看看