Minimum number of clicks to solve Flood-It-like puzzle

前端 未结 2 1990
孤街浪徒
孤街浪徒 2021-01-02 05:16

I have grid N × M in which each cell is coloured with one colour.

When the player clicks on any cell of the grid of colour α, the cell in the top-leftmost corner of

相关标签:
2条回答
  • 2021-01-02 06:11

    High level improvement

    Note that the cells are either their original colour, or the last selected colour.

    This means that we can represent the current state of the board by means of 20 bits (marking for each of the 4*5 cells whether they contain the original colour), and a number in the range 0 to 9 giving the last colour chosen.

    This results in a maximum of 10 million states to explore. The backtracking function can avoid having to recurse if it reaches a state that has already been visited. I would expect this change to make your solution considerably faster.

    Low level improvement

    Representing the state by a 20bit mask and the last colour also makes the state a lot quicker to update and restore as only 2 numbers need to be changed instead of memcpy of the whole board.

    0 讨论(0)
  • 2021-01-02 06:18

    If you think of the 4x5 board as "19 squares you can click on", that suggests there are 19! or 121,645,100,408,832,000 combinations to check. However, there are a number of optimisations that can drastically reduce the number of combinations to only a few dozen:

    observations about the game strategy

    • Clicking on different squares with the same colour has the same effect. So the board should be seen as "9 colours (or less) you can click on".
    • Adjacent squares with the same colour should be seen as groups; they always act together. In the example below, the three white squares at the bottom right form such a group.
    • It only makes sense to click on a colour that is adjacent to the corner group. In step one of the example below, only clicking on a pink, green, orange or white square makes sense.
    • When several uniquely-coloured groups (where only one group has a certain colour) are adjacent to the corner group, the order in which they are clicked is unimportant. In the example below, after 5 and 3 have been clicked, any order of clicking 4, 7, 8 and 9 will have the same result.
    • When all groups of a certain colour are adjacent to the corner group, they can be seen as uniquely-coloured groups; exactly one click per colour is needed to connect them. In the example below, after clicking 5 and 3, the two pink squares and the two green squares become two uniquely-coloured groups
    • When the board has only uniquely-coloured groups, the number of necessary clicks equals the number of unconnected groups. In the example below, after clicking 5 and 3, exactly eight more clicks are necessary.
    • If there is exactly one unconnected group that has the same colour as the corner group, and they are separated by more than one other group, the group can be seen as a uniquely-coloured group.
    • Reducing the number of clicks means connecting several groups at once. This can be done when several same-coloured groups are adjacent to the corner group (as when clicking 1 or 2 in step 3 of the example below), or by clicking a group which separates the corner group from a group with the same colour (as happens in steps 1 and 2 in the example).

    Algorithm based on optimal strategy

    A recursive algorithm based on an optimal strategy using the rules listed above, goes through these steps for every recursion:

    1. If only uniquely-coloured groups are left, the number of clicks equals the number of unconnected groups; return this number.
    2. If a group with the same colour as the corner-group is separated from it by only one other group, click this other group and recurse. If several options exist, try all of them.
    3. If one or more uniquely-coloured groups (or all groups of certain colours) are adjacent to the corner group, click them all in any order. Then re-evaluate the board from step 1.
    4. Try clicking every colour adjacent to the corner group and recurse.

    A Javascript implementation of a brute-force algorithm needed tens of millions of recursions for example 1 and 3 in the question, with an execution time far above the 8 second limit. After implementing the optimisations described above, example 1 was solved with just 38 recursions, and an execution time of a few milliseconds. Examples 2 and 3 were even quicker.

    0 讨论(0)
提交回复
热议问题