1 public class Main { 2 /*** 3 * ___====-_ _-====___ 4 * _--^^^#####// \\#####^^^--_ 5 * _-^##########// ( ) \\##########^-_ 6 * -############// |\^^/| \\############- 7 * _/############// (@::@) \\############\_ 8 * /#############(( \\// ))############# 9 * -###############\\ (oo) //###############- 10 * -#################\\ / VV \ //#################- 11 * -###################\\/ \//###################- 12 * _#/|##########/\######( /\ )######/\##########|\#_ 13 * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| 14 * ` |/ V V ` V \#\| | | |/#/ V ‘ V V \| ‘ 15 * ` ` ` ` / | | | | \ ‘ ‘ ‘ ‘ 16 * ( | | | | ) 17 * __\ | | | | /__ 18 * (vvv(VVV)(VVV)vvv) 19 * 神兽保佑 20 * 代码无BUG! 21 */ 22 23 static int compare(int a, int b, int c) { 24 if(b == 0) return Math.min(a, c); 25 int temp = Math.min(a, b); 26 return Math.min(temp, c); 27 28 } 29 public static void main(String args[]) { 30 Scanner sc = new Scanner(System.in); 31 int num = sc.nextInt(); 32 int[][] time = new int[num+1][3]; //存放时间 33 int[] high = new int[num+1]; //存放楼层高度 34 int count = 0; 35 for(int i = 1; i <= num; i++) { 36 high[i] = sc.nextInt(); 37 count += high[i]; 38 } 39 40 Arrays.fill(time[0], count); 41 for(int i = 0; i < num; i++) { 42 43 time[i+1][0] = time[i][2] - high[i+1]; //第一列为上一次跳一层后,用的总时间 44 if(i < num-1) 45 time[i+2][1] = time[i][2] - high[i+1] - high[i+2]; //第二列为上一次跳两层后,用的总时间 46 47 time[i+1][2] = compare(time[i][0], time[i][1], time[i][2]); //第三列为爬一层后用的总时间,比较后赋值 48 } 49 int result = Math.min(time[num][0], time[num][1]); 50 System.out.print(Math.min(result, time[num][2])); //输出最后一层用的总时间 51 52 } 53 }
如果有大佬有其他方法,希望能提供代码参考,谢谢,DP不是很会。
原文:https://www.cnblogs.com/ohuo/p/12330932.html