题目:
思路,就每次两个门之间就两条路 用dp 储存每次走的时间, 来计算最短时间
代码:
package 蓝桥杯备赛;
import java.util.Scanner;
public class 蜗牛 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n;
n = scan.nextInt();
int []zhuzi = new int[n];
for(int i=0;i<n;i++) {
zhuzi[i] = scan.nextInt();
}
int []chuanru = new int[n];
int []chuanchu = new int[n];
for (int i=0;i<n-1;i++) {
chuanru[i] = scan.nextInt();
chuanchu[i] = scan.nextInt();
}
double [][]dp = new double[n][2];//n行2列
dp[0][0] = zhuzi[0];//从原点爬到第一个柱子必须因外不论如何也要到终点
dp[0][1] = zhuzi[0]+chuanru[0]/0.7;
for(int i = 1;i<n;i++) {
//到第i根底部的路径只能是两种,一种是下滑1.3的速度坐传送门,另从底部直接爬过去
dp[i][0]=Math.min(dp[i-1][1]+chuanchu[i-1]/1.3,dp[i-1][0]+zhuzi[i]-zhuzi[i-1]);
if(chuanru[i]>chuanchu[i-1]) {
dp[i][1] = Math.min(dp[i-1][1]+(chuanru[i]-chuanchu[i-1])/0.7,dp[i][0]+chuanru[i]/0.7);//从底部往上爬和传送门出来上下爬两种
}
else {
dp[i][1] = Math.min(dp[i-1][1]+(chuanchu[i-1]-chuanru[i])/1.3,dp[i][0]+chuanru[i]/0.7);
}
}
System.out.printf("%.2f",dp[n-1][0]);
}
}