Description
Mirko has a chessboard with N rows and just three columns. Slavica has written an integer on each field. Mirko has K dominoes at his disposal, their dimensions being 2x1, and has to arrange all of them on the board without overlapping, in a way that each domino covers exactly two fields of the board. He can rotate the dominoes as he pleases.
Help Mirko cover the largest sum of numbers possible with the dominoes!
Input
The first line of input contains the integer N (1 ≤ N ≤ 1000), the number of rows, and K (1 ≤ K ≤ 1000), the number of dominoes available. Each of the following N lines contains three integers written in the ith row of the board. All numbers will be lesser than 10^6 by absolute value
Output
The first and only line of output must contain the maximal sum possible to cover with exactly K dominoes.
Sample Input
5 3 2 1 -1 1 3 2 0 2 3 2 1 1 3 3 0
Sample Output
16
题意:给你一个n*3的矩阵,每一个格子都有对应的价值,让你在里面恰好铺满k块1*2的砖,可以竖着铺,也可以横着铺,问铺完k块砖后所能覆盖到的最大价值是多少。
思路:与poj2411全部铺满不同,这题可以不铺满,所以我们可以考虑每一层状态里0代表空缺没有铺,1代表这个格子铺过了,那么对于每一层,我们可以枚举这一层铺的状态,如果这一层有格子是竖着放的,那么我们要看看上一层的当前这个位置是不是0,如果是0就可以竖着放,我们枚举每一层的状态的时候,只考虑铺在当前这一行的格子以及可能占用上一层的砖,不考虑延伸到下一层的砖。这样我们就可以设dp[i][j][state]表示当前为i行,已经铺了k个砖,当前行的状态为state的能覆盖到的最大价值。
#include #include #include #include #include #include #include