Given a chess board with 4 rows and n
columns. On every cell there's an integer. Given 2n
discs that part of them, or all of them you can put on a different cell on the board so the total sum is maximal. The only limitation is that 2 discs can't be next to each other vertically or horizontally. How to place the best combination of discs on the board in O(n)
using DP ?

- 50,140
- 28
- 121
- 140

- 13
- 4
-
So I've said that for each column there are 7 possibility patterns (not placing a disc isn't an option cause question says part of them) . I made a matrix M that will output T or F if I can place a column classified as c(which can be 0 to 6) next to column i+1 . so I thought about arrange n priority queues sized 7 and each element in the queue is a pair of the classify and the sum of the numbers from that classify , right here i'm stuck – itorra May 19 '15 at 20:19
-
Why use a priority queue? Try to think about how you would formulate this as a DP. You want to find a recurrence that describes how you can build the solution. Start simple: how would you formulate the DP if there is no restriction on where we can place the discs in each column? – Jordi Vermeulen May 19 '15 at 20:32
-
so I thought about sum(n) = sum(n-1) + max {pik : k from 0 to number of possible patterns} . (p - current column) The issue is whenever I need to insert the condition I can't go backward change the whole subproblem – itorra May 19 '15 at 20:37
-
Right, so you need some way to know what the last column was in the recursive call... also note that you need a way to make sure you're not using more than 2n discs. – Jordi Vermeulen May 19 '15 at 20:43
-
memorization? I can always keep aside the classify of the subproblem, but what do I do if want to change the subproblem itself.. well that's the point where I'm really deep in the mud with this question. I will never use more than 2*n discs because I already calculated the sum of every possible pattern . – itorra May 19 '15 at 20:48
-
What do you mean with "change the subproblem"? You always consider all subproblems, then pick the one that gives you an optimal solution. – Jordi Vermeulen May 19 '15 at 20:53
-
How can I know that subproblem will always work with the new column ? – itorra May 19 '15 at 21:03
-
By not building your solution using a subproblem that conflicts with your choice for the new column. – Jordi Vermeulen May 19 '15 at 21:04
1 Answers
1st, we cannot possibly use more than 2*n disk as any column could contain at maximum 2 disks.
let's say d[i][mask] - maximum amount obtained after filling columns from 1 to i with disks so that the last column is filled as mask ( mask could be 0000, 0001, 0010, 0100, 0101, 1000, 1001 or 1010 where 1 means disk was placed and 0 means it wasn't)
So d[i][mask1] = max of (d[i-1][mask2] + number obtained from applying mask1 on i-th column), where mask2 could be any mask which doensn't contradict with mask1
Edit 1: You do not need to change anything. When you are on i-th step on certain mask, it only depends on the answers for masks of i-1. And you already have all of them. You just try to update the current d[i][mask] from those which are valid. As I already said d[i][mask] - stores the maximum value which could be obtained filling columns from 1 to i, optimally, so that the last column has disks in form of this mask (doesn't matter masks before the i-th column were filled, because they won't affect next column)

- 2,891
- 5
- 32
- 50
-
let's say there's a very big number obtained from applying mask1 on the i-th column, and mask1 contradict with mask2 , where do I check how I can change everything in the subproblem so I can have this number in d[i][mask1] ? – itorra May 20 '15 at 17:05
-
@itorra updated my answer. and maybe if you've got an example I can explain on it – Herokiller May 21 '15 at 02:59