一个数塔问题,以时间为纵坐标、位置为横坐标创建一个二维数组,然后从下往上相加。
状态转移方程:9>= j>=1时 dp[i][j]+=max(max(dp[i+1][j],dp[i+1][j+1]),dp[i+1][j-1])
j=0时 dp[i][j] += max(dp[i+1][j],dp[i+1][j+1])
j=10时 dp[i][j] += max(dp[i+1][j],dp[i+1][j-1])
import java.util.Scanner; public class hdu1176 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); if (n==0) { break; } int[][] aa = new int[100005][11]; int time = 0; for (int i = 0; i < n; i++) { int place = sc.nextInt(); int sec = sc.nextInt(); aa[sec][place]++; time = Math.max(time, sec); } for (int i = time-1; i >= 0; i--) { for (int j = 0; j <= 10; j++) { if (j==0) { aa[i][j] += Math.max(aa[i+1][j], aa[i+1][j+1]); }else if (j==10) { aa[i][j] += Math.max(aa[i+1][j], aa[i+1][j-1]); }else { aa[i][j] += Math.max(Math.max(aa[i+1][j], aa[i+1][j+1]),aa[i+1][j-1]); } } } System.out.println(aa[0][5]); } sc.close(); } }
标签:java,int,max,time,馅饼,sec,hdu1176,sc,dp From: https://www.cnblogs.com/xiaohuangTX/p/18197000