Bomb dropping algorithm

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

    It seems that a linear programming approach can be very helpful here.

    Let Pm x n be the matrix with the values of the positions:

    Matrix of positions

    Now let define a bomb matrix B(x, y)m x n,with 1 ≤ x ≤ m, 1 ≤ y ≤ n as below

    Bomb matrix

    in such a way that

    Values of positions in bomb matrix

    For example:

    B(3, 3)

    So we are looking to a matrix Bm x n = [bij] that

    1. Can be defined as a sum of bomb matrices:

      B as a sum of bomb matrices

      (qij would be then the quantity of bombs we would drop in position pij)

    2. pij - bij ≤ 0 (to be more succint, let us say it as P - B ≤ 0)

    Also, B should minimize the sum sum of quantities of bombs.

    We can also write B as the ugly matrix ahead:

    B as a matrix of sum of quantities

    and since P - B ≤ 0 (which means P ≤ B) we have the following pretty linear inequality system below:

    Relationship between number of bombs dropped and values in positions

    Being qmn x 1 defined as

    Vector of quantities

    pmn x 1 defined as

    Values of P distributed as a vector

    We can say we have a system The system below represented as product of matrices http://latex.codecogs.com/gif.download?S%5Cmathbf%7Bq%7D&space;%5Cge&space;%5Cmathbf%7Bp%7D being Smn x mn the matrix to be reversed to solve the system. I did not expand it myself but I believe it should be easy to do it in code.

    Now, we have a minimum problem which can be stated as

    The system we have to solve

    I believe it is something easy, almost trivial to be solved with something like the simplex algorithm (there is this rather cool doc about it). However, I do know almost no linear programming (I will take a course about it on Coursera but it is just in the future...), I had some headaches trying to understand it and I have a huge freelance job to finish so I just give up here. It can be that I did something wrong at some point, or that it can't go any further, but I believe this path can eventually lead to the solution. Anyway, I am anxious for your feedback.

    (Special thanks for this amazing site to create pictures from LaTeX expressions)

提交回复
热议问题