Dynamic Programming and Knapsack Application

前端 未结 4 1677
遇见更好的自我
遇见更好的自我 2020-12-29 00:26

Im studying dynamic programming and am looking to solve the following problem, which can be found here http://www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf:

You

相关标签:
4条回答
  • 2020-12-29 00:43

    Please include the necessary conditions for rectangle of size (0, something) or (something, 0) in the Rect() function.

    0 讨论(0)
  • 2020-12-29 00:57

    optimize() {

    Rectangle memo[width][height]
    optimize(0,0,totalwidth, totalheight, memo)
    

    }

    optimize(x, y, width, height, memo) {

    if memo[width][height] != null
        return memo[width][height]
    
    rect = new Rectangle(width, height, value = 0)
    for each pattern {
    
        //find vertical cut solution
        leftVerticalRect = optimize (x, y + pattern.height, pattern.width, height-pattern.height,memo)
        rightVerticalRect = optimize(x  + pattern.width, y, width-pattern.width, height)
        verticalcut = new Cut(x + pattern.width, y, x + pattern.width, y + height)
    
        //find horizontal cut solution
        topHorizontalRect = optimize ( --parameters-- )
        bottomHortizonalRect = optimize( --parameters--)
        horizontalcut = new Cut( --parameters--)
    
        //see which solution is more optimal
        if (leftVerticalRect.val + rightVerticalRect.val > topHorizontalRect.val + bottomHorizontalRect.val)
            subprobsolution = vertical cut solution
        else
            subprobsolution = horizontal cut solution
    
        //see if the solution found is greater than previous solutions to this subproblem
        if (subprobsolution.value + pattern.value > rect.value) {
            rect.subrect1 = subprobsolutionrect1
            rect.subrect2 = subprobsolutionrect2
            rect.pattern = pattern
            rect.cut = subprobsolutioncut
            rect.value = rect.subrect1.value + rect.subrect2.value + rect.pattern.value
        }
    }
    
    memo[width][height] = rect
    return rect
    

    }

    0 讨论(0)
  • 2020-12-29 01:01

    I think you should focus on the fact that the machine cuts the cloth into two pieces. What can fit in each of those two pieces? Consider the following:

    +-------------+-------------------+
    |             |       Piece B     |
    |             |                   |
    |  Piece A    +----------+--------+
    |             |          |        |
    |             |          |        |
    |             |          | Piece  |
    +-------------+----------+   C    |
    |                        |        |
    |         Piece D        |        |
    +------------------------+--------+
    

    These four fit in the cloth, but not in a way that's possible to achieve with three cuts. (Possibly a different arrangement would allow that with these particular pieces; think of this as a conceptual diagram, not to scale. My ascii art skills are limited today.)

    Focussing on "where is the cut" should give you the dynamic programming solution. Good luck.

    0 讨论(0)
  • 2020-12-29 01:06

    So you start with a X * Y rectangle. Say the optimal solution involves making a vertical (or horizontal) cut, then you have two new rectangles with dimensions X * Y1 and X * Y2 with Y1 + Y2 = Y. Since you want to maximize your profit, you need to maximize the profit on these new rectangles (optimal substructure). So your initial recursion goes as follows: f(X, Y) = max(f(X, Y1) + f(X, Y2), f(X1, Y) + f(X2, Y)) for all posible values of X1, X2 (horizontal cut) and Y1, Y2 (vertical cut).

    Now the question is when do I actually decide to make a product ? You can decide to make a product when one of its dimensions equals one of the dimensions of your current rectangle (why ? Because if this doesn't hold, and the optimal solution includes making this product, then sooner or later you will need to make a vertical (or horizontal) cut and this case is already handled in the initial recursion), so you make the appropriate cut and you have a new rectangle X * Y1 (or X1 * Y), depending on the cut you made to obtain the product), in this case the recursion becomes f(X, Y) = cost of product + f(X1, Y).

    The solution of the original problem is f(X, Y). The running time of this dp solution would be O(X * Y * (X + Y + number of available products)): you have X * Y possible rectangles, for each of these you try every possible cut (X + Y) and you try to make one of the available products out of this rectangle.

    Also, check out this similar problem: Sharing Chocolate from the 2010 ICPC World Finals.

    0 讨论(0)
提交回复
热议问题