Bomb dropping algorithm

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

    1. Never bomb border (unless square does not have nonborder neighbour)
    2. Zero corner.
    3. To zero corner, drop value of corner one square away diagonaly (the only nonborder neighbour)
    4. This will create new corners. Go to 2

    Edit: did not notice that Kostek suggested almost same approach, so now I make stronger claim: If corners to clear are chosen to be always on outermost layer, then it is optimal.

    In OP's example: dropping 2 (as 1+1 or 2) on anything else than on 5 does not leads to hitting any square that dropping on 5 would hit. So we simply must drop 2 on 5 (and 6 on lower left 1 ...)

    After this, there is only one way how to clear (in top left) corner what was originaly 1 (now 0), and that is by dropping 0 on B3 (excel like notation). And so on.

    Only after clearing whole A and E columns and 1 and 7 rows, start clearing one layer deeper.

    Consider cleared only those intentionaly cleared, clearing 0 value corners costs nothing and simplifies thinking about it.

    Because all bombs dropped this way must be dropped and this leads to cleared fields, it is optimal solution.


    After good sleep I realized that this is not true. Consider

      ABCDE    
    1 01000
    2 10000
    3 00000
    4 00000
    

    My approach would drop bombs on B3 and C2, when dropping on B2 would be enough

提交回复
热议问题