Search code examples
cbaduk

Fast algorithm to clear a board in Go


I am currently writing some code to deal with Go boards. A Go board is represented as an array of colors. The array has size × size entries and represents a two-dimensional square board.

enum color {
    EMPTY,
    BLACK,
    WHITE,
};

struct go_board {
    unsigned int size;
    enum color intersections[];
};

When enum color player moves, the following procedure applies: (See rules)

  1. [...]
  2. a point P, not colored C, is said to reach C, if there is a path of (vertically or horizontally) adjacent points of P's color from P to a point of color C.
  3. Clearing a color is the process of emptying all points of that color that don't reach empty.
  4. [...]
  5. A move consists of [...] clearing the opponent color, and then clearing one's own color.

I am looking for a fast (in terms of noth computational complexity and actual speed) algorithm to clear a board. Can you help me?


Solution

  • Use image processing's flood-filling algorithms. First, seed with the empty points and fill all positions that are white or empty; all non-filled positions with white stones will be dead. Repeat with black.