代码:
public static BigDecimal squGetNum() {
Scanner cin = new Scanner(System.in);
int row = cin.nextInt();
int column = cin.nextInt();
BigDecimal[][] dpLong = new BigDecimal[100][100];
BigDecimal ans = new BigDecimal(0);
int[] sq = new int[100];
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= column; j++) {
sq[j] = cin.nextInt();
}
for (int j = 1; j <= column; j++) {
dpLong[j][j] = BigDecimal.valueOf(sq[j] * 2);
}
// f[i][j]=max(f[i+1][j]+num[i]*2^(m-i), f[i][j-1]+num[j]*2^(m-i))
for (int j = column - 1; j > 0; j--) {
BigDecimal max = BigDecimal.valueOf(0);
BigDecimal lastNum;
for (int k = j + 1; k <= column; k++) {
// dpLong[j + 1][k] < dpLong[j][k - 1]
if (dpLong[j][k - 1].compareTo(dpLong[j + 1][k]) > 0) {
max = dpLong[j][k - 1];
lastNum = dpLong[k][k];
} else {
max = dpLong[j + 1][k];
lastNum = dpLong[j][j];
}
dpLong[j][k] = max.multiply(BigDecimal.valueOf(2)).add(lastNum);
}
}
// System.out.println(dpLong[1][column]);
ans = ans.add(dpLong[1][column]);
// System.out.println(ans);
}
//1 4
//4 5 0 5
return ans;
}
状态表:从下到上,从左到右,依次填入状态
0 | 4 | 5 | 0 | 5 | ||
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 0 | 4*2 | 4*2+5*4(第3步) | 0*2+4*4+5*8(第4步) | 5*2+0*4+4*8+5*16(第5步) | |
5 | 0 | 5*2 | 0*2+5*4(第2步) | 5*2+0*4+5*8(第3步) | ||
0 | 0 | 0*2 | 0*2+5*4(第一步) | |||
5 | 0 | 5*2 | ||||
过了4个ac不知道哪里错了,希望大牛能指点下 |