动态规划解决hdu龟兔赛跑

import java.util.Scanner;

class test {
    private double dp[];//用来存储每段的最短时间
    private double p[];//用来存储每个充电站点离起点的距离
    private int n,c,t;
    private double vr,vt1,vt2;
    private double Length;
    
    public boolean Solution(){
        System.out.print("请输入每个站点离起点的距离:");
        Scanner iner=new Scanner(System.in);
        for(int i=1;i<=this.n;++i){
            p[i]=iner.nextInt();
        }
        double curTime=0;
        for(int i=1;i<=this.n+1;++i){
            double minTime=100000.0;
          for(int j=0;j<i;++j){
            if(c>p[i]){   //判断充满电后的距离能不能直接过站点
                curTime=p[i]/vt1;
            }else{
                curTime=c/vt1+(p[i]-c)/vt2;   //不加油的时间
            }
            //过站点时要分两种情况--一是加油,二是不加油
            if(j>0){    //考虑加油的情况
                 //1.加油时间上,加t秒
                 //2.提速时间上,减x秒
                 curTime=dp[i-1];
                 curTime+=t;
                if(p[i]-p[i-1] > c){
                  curTime+=c/vt1+(p[i]-p[i-1]-c)/vt2;
                }
                else{
                  curTime+=(p[i]-p[i-1])/vt1;
                }
            }
            if(curTime<minTime){     //然后再对两者进行比较
                minTime=curTime;
            }
          }
          dp[i]=minTime; 
        }
        int Time= (int) (this.Length / vr);
        if(dp[this.n+1]<Time){
            System.out.println("What a pity rabbit!");
        }else{
            System.out.println("Good job,rabbit!");
        }
        iner.close();
        return true;
    }
    
    public test(int l,int x,int y,int z,int k,int m,int n){
      this.Length=l;
      this.n=x;this.vr=k;
      this.c=y;this.vt1=m;
      this.t=z;this.vt2=n;
      p=new double[this.n+2];
      dp=new double[this.n+2];
      p[0]=0;p[this.n+1]=l;
    }
}



public class rabbit{
    public static void main(String[] args) {
        test space=new test(100,3,5,1,10,20,5);
        space.Solution();
    }
}

 

posted @ 2019-10-05 13:49  pycodego  阅读(246)  评论(0编辑  收藏  举报