Beispiel #1
0
        /* --------------------------------------------------------------------------------- *\
        *  Description:
        *   Paint a single isle using DFS to find all relevant pixels
        *
        *  Parameters:
        *   int n_src_x:
        *   int n_src_y:
        *   int n_pixel_ind:
        *   int n_color:
        *   CBoard _board:
        \* --------------------------------------------------------------------------------- */
        private static void paint_isle(int n_src_x, int n_src_y, int n_pixel_ind, int n_color, CBoard _board)
        {
            Stack <CPathNode> _path = new Stack <CPathNode>();

            paint_n_push(n_src_x, n_src_y, n_pixel_ind, n_color, _board, _path);

            while (0 != _path.Count)
            {
                bool b_is_painted = false;

                CPathNode _path_node = _path.Peek();
                int       n_center_x = _path_node.x;
                int       n_center_y = _path_node.y;

                for (int i = _path_node.next_neighbour_ind; i < ma_neighbours.Length; i++)
                {
                    int n_neighbour_x = n_center_x + ma_neighbours[_path_node.next_neighbour_ind].Item1;
                    int n_neighbour_y = n_center_y + ma_neighbours[_path_node.next_neighbour_ind].Item2;

                    _path_node.next_neighbour_ind++;

                    if (0 > n_neighbour_x || _board.width <= n_neighbour_x)
                    {
                        continue;
                    }
                    if (0 > n_neighbour_y || _board.height <= n_neighbour_y)
                    {
                        continue;
                    }

                    int n_neighbour_ind = _board.pixel_ind(n_neighbour_x, n_neighbour_y);

                    if (_board.is_black_pixel(n_neighbour_ind))
                    {
                        paint_n_push(n_neighbour_x, n_neighbour_y, n_neighbour_ind, n_color, _board, _path);
                        b_is_painted = true;
                        break;
                    }
                }

                if (b_is_painted)
                {
                    continue;
                }

                _path.Pop();
            }
        } // paint_isle()
Beispiel #2
0
        /* --------------------------------------------------------------------------------- *\
        *  Description:
        *   Paint and count all islands
        *
        *  Parameters:
        *   CBoard _board:
        *   CColorGenerator _color_generator:
        *
        *  Returns int:
        \* --------------------------------------------------------------------------------- */
        public static int paint(CBoard _board, CColorGenerator _color_generator)
        {
            int n_isle_count = 0;

            for (int y = 0; y < _board.height; y++)
            {
                for (int x = 0; x < _board.width; x++)
                {
                    int n_pixel_ind = _board.pixel_ind(x, y);

                    if (!_board.is_black_pixel(n_pixel_ind))
                    {
                        continue;
                    }

                    n_isle_count++;

                    paint_isle(x, y, n_pixel_ind, _color_generator.generate_color(), _board);
                }
            }

            return(n_isle_count);
        }