Bomb dropping algorithm

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

    Here's my solution.. I won't write it out in code yet since I don't have time, but I believe this should produce an optimal number of moves each time - though I'm not sure how efficient it would be at finding the points to bomb.

    Firstly, as @Luka Rahne stated in one of the comments, the order in which you bomb is not important- only the combination.

    Secondly, as many others have stated, bombing 1-off the diagonal from the corners is optimal because it touches more points than the corners.

    This generates the basis for my version of the algorithm: We can bomb the '1-off from the corners' first or last, it doesn't matter (in theory) We bomb those first because it makes later decisions easier (in practice) We bomb the point which affects the most points, while simultaneously bombing those corners.

    Let's define Points Of Resistance to be the points in the board with the most non-bombable points + largest number of 0's around them

    non-bombable points can be defined as points which don't exist in our current scope of the board we're looking at.

    I'll also define 4 bounds which will handle our scope: Top=0, Left=0, Bottom=k,right=j. (values to start)

    Finally, I'll define optimal bombs as bombs which are dropped on points that are adjacent to points of resistance and are touching (1) the highest valued point of resistance and (2) the largest number of points possible.

    Regarding the approach- it's obvious we're working from the outside in. We will be able to work with 4 'bombers' at the same time.

    The first points of resistance are obviously our corners. The 'out of bound' points are not bombable (there are 5 points outside the scope for each corner). So we bomb the points diagonally one off the corners first.

    Algorithm:

    1. Find the 4 optimal bomb points.
    2. If a bomb point is bombing a resistance point which is touching 2 bounds (i.e. a corner), bomb till that point is 0. Otherwise, bomb each until one of the points of resistance touching the optimal bomb point is 0.
    3. for each bound: if(sum(bound)==0) advance bound

    repeat until TOP=BOTTOM and LEFT=RIGHT

    I will try to write the actual code later

提交回复
热议问题