Bomb dropping algorithm

后端 未结 30 1214
挽巷
挽巷 2021-01-29 16:55

I have an n x m matrix consisting of non-negative integers. For example:

2 3 4 7 1
1 5 2 6 2
4 3 4 2 1
2 1 2 4 1
3 1 3 4 1
2 1 4 3 2
6 9 1 6 4
         


        
30条回答
  •  攒了一身酷
    2021-01-29 17:33

    I had to stop at only a partial solution since I was out of time, but hopefully even this partial solution provides some insights on one potential approach to solving this problem.

    When faced with a hard problem, I like to come up with simpler problems to develop an intuition about the problem space. Here, the first step I took was to reduce this 2-D problem into a 1-D problem. Consider a line:

    0 4 2 1 3 0 1
    

    Somehow or another, you know you will need to bomb at or around the 4 spot 4 times to get it down to 0. Since left of the spot is a lower number, there is no benefit to bombing the 0 or the 4 over bombing the 2. In fact, I believe (but lack a rigorous proof) that bombing the 2 until the 4 spot goes down to 0 is at least as good as any other strategy to get that 4 down to 0. One can proceed down the line left to right in a strategy like this:

    index = 1
    while index < line_length
      while number_at_index(index - 1) > 0
        bomb(index)
      end
      index++
    end
    # take care of the end of the line
    while number_at_index(index - 1) > 0
      bomb(index - 1)
    end
    

    A couple sample bombing orders:

    0 4[2]1 3 0 1
    0 3[1]0 3 0 1
    0 2[0]0 3 0 1
    0 1[0]0 3 0 1
    0 0 0 0 3[0]1
    0 0 0 0 2[0]0
    0 0 0 0 1[0]0
    0 0 0 0 0 0 0
    
    4[2]1 3 2 1 5
    3[1]0 3 2 1 5
    2[0]0 3 2 1 5
    1[0]0 3 2 1 5
    0 0 0 3[2]1 5
    0 0 0 2[1]0 5
    0 0 0 1[0]0 5
    0 0 0 0 0 0[5]
    0 0 0 0 0 0[4]
    0 0 0 0 0 0[3]
    0 0 0 0 0 0[2]
    0 0 0 0 0 0[1]
    0 0 0 0 0 0 0
    

    The idea of starting with a number that needs to go down some way or another is an appealing one because it suddenly becomes attainable to find a solution that as some claim to being at least as good as all other solutions.

    The next step up in complexity where this search of at least as good is still feasible is on the edge of the board. It is clear to me that there is never any strict benefit to bomb the outer edge; you're better off bombing the spot one in and getting three other spaces for free. Given this, we can say that bombing the ring one inside of the edge is at least as good as bombing the edge. Moreover, we can combine this with the intuition that bombing the right one inside of the edge is actually the only way to get edge spaces down to 0. Even more, it is trivially simple to figure out the optimal strategy (in that it is at least as good as any other strategy) to get corner numbers down to 0. We put this all together and can get much closer to a solution in the 2-D space.

    Given the observation about corner pieces, we can say for sure that we know the optimal strategy to go from any starting board to a board with zeros on all corners. This is an example of such a board (I borrowed the numbers from the two linear boards above). I've labelled some spaces differently, and I'll explain why.

    0 4 2 1 3 0 1 0
    4 x x x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    

    One will notice at the top row really closely resembles the linear example we saw earlier. Recalling our earlier observation that the optimal way to get the top row all down to 0 is to bomb the second row (the x row). There is no way to clear the top row by bombing any of the y rows and no additional benefit to bombing the top row over bombing the corresponding space on the x row.

    We could apply the linear strategy from above (bombing the corresponding spaces on the x row), concerning ourselves only with the top row and nothing else. It would go something like this:

    0 4 2 1 3 0 1 0
    4 x[x]x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    
    0 3 1 0 3 0 1 0
    4 x[x]x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    
    0 2 0 0 3 0 1 0
    4 x[x]x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    
    0 1 0 0 3 0 1 0
    4 x[x]x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    
    0 0 0 0 3 0 1 0
    4 x x x x x x 4
    2 y y y y y y 2
    1 y y y y y y 1
    3 y y y y y y 3
    2 y y y y y y 2
    1 y y y y y y 1
    5 y y y y y y 5
    0 4 2 1 3 0 1 0
    

    The flaw in this approach becomes very obvious in the final two bombings. It is clear, given that the only bomb sites that reduce the 4 figure in the first column in the second row are the first x and the y. The final two bombings are clearly inferior to just bombing the first x, which would have done the exact same (with regard to the first spot in the top row, which we have no other way of clearing). Since we have demonstrated that our current strategy is suboptimal, a modification in strategy is clearly needed.

    At this point, I can take a step back down in complexity and focus just one one corner. Let's consider this one:

    0 4 2 1
    4 x y a
    2 z . .
    1 b . .
    

    It is clear the only way to get the spaces with 4 down to zero are to bomb some combination of x, y, and z. With some acrobatics in my mind, I'm fairly sure the optimal solution is to bomb x three times and then a then b. Now it's a matter of figuring out how I reached that solution and if it reveals any intuition we can use to even solve this local problem. I notice that there's no bombing of y and z spaces. Attempting to find a corner where bombing those spaces makes sense yields a corner that looks like this:

    0 4 2 5 0
    4 x y a .
    2 z . . .
    5 b . . .
    0 . . . .
    

    For this one, it is clear to me that the optimal solution is to bomb y 5 times and z 5 times. Let's go one step further.

    0 4 2 5 6 0 0
    4 x y a . . .
    2 z . . . . .
    5 b . . . . .
    6 . . . . . .
    0 . . . . . .
    0 . . . . . .
    

    Here, it feels similarly intuitive that the optimal solution is to bomb a and b 6 times and then x 4 times.

    Now it becomes a game of how to turn those intuitions into principles we can build on.

    Hopefully to be continued!

提交回复
热议问题