Bomb dropping algorithm

后端 未结 30 836
挽巷
挽巷 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:46

    This is a partial answer, I'm trying to find a lower bound and upper bound that could be the possible number of bombs.

    In 3x3 and smaller board, the solution is trivially always the largest numbered cell.

    In boards larger than 4x4, the first obvious lower bound is the sum of the corners:

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

    however you arrange the bomb, it is impossible to clear this 4x4 board with less than 2+1+6+4=13 bombs.

    It has been mentioned in other answers that placing the bomb on the second-to-corner to eliminate the corner is never worse than placing the bomb on the corner itself, so given the board:

    *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*
    

    We can zero the corners out by placing bombs on the second-to-corner to give a new board:

     0  1  1  6  0
     0  3  0  5  1
     2  1  1  1  0
     2  1  2  4  1
     0  0  0  0  0
     0  0  0  0  0
     0  3  0  2  0
    

    So far so good. We need 13 bombs to clear the corners.

    Now observe the number 6, 4, 3, and 2 marked below:

     0  1  1 *6* 0
     0  3  0  5  1
     2  1  1  1  0
    *2* 1  2 *4* 1
     0  0  0  0  0
     0  0  0  0  0
     0 *3* 0  2  0
    

    There is no way to bomb any two of those cells using a single bomb, so the minimum bomb has increased by 6+4+3+2, so adding to the number of bombs we used to clear the corners, we get that the minimum number of bombs required for this map has become 28 bombs. It is impossible to clear this map with less than 28 bombs, this is the lower bound for this map.

    You can use greedy algorithm to establish an upper bound. Other answers have shown that a greedy algorithm produces a solution that uses 28 bombs. Since we've proven earlier that no optimal solution can have less than 28 bombs, therefore 28 bombs is indeed an optimal solution.

    When greedy and the method to find the minimal bound I've mentioned above does not converge though, I guess you do have to go back to checking all combinations.

    The algorithm for finding the lower bound is the following:

    1. Pick an element with the highest number, name it P.
    2. Mark all cells two steps away from P and P itself as unpickable.
    3. Add P to the minimums list.
    4. Repeat to step 1 until all cells are unpickable.
    5. Sum the minimums list to get the lower bound.

提交回复
热议问题