Exemple #1
0
        private void Search(DancingLinks.HeaderNode pHeader)
        {
            //If our matrix is empty we have our solution
            if (pHeader == _links.Root)
            {
                if (SolutionFound != null)
                {
                    SolutionFound(BuildSolution());
                }
            }
            else
            {
                //Cover this header
                pHeader.Cover();

                //Cycle through the column
                DancingLinks.Node column = pHeader.Down;
                while (column != pHeader)
                {
                    //Add the row to our solution
                    _solutionList.Add(column);

                    //Add the row to our solution and cover the columns in this row
                    DancingLinks.Node row = column.Right;
                    while (row != column)
                    {
                        row.Header.Cover();
                        _solutionList.Add(row);
                        row = row.Right;
                    }

                    //Recurse with our reduced matrix
                    Search(ChooseHeaderNode());
                    _solutionList.Remove(column);

                    //Uncover the columns in the row to return our matrix to normal so recursion doesn't break
                    row = column.Left;
                    while (row != column)
                    {
                        row.Header.Uncover();
                        _solutionList.Remove(row);
                        row = row.Left;
                    }
                    column = column.Down;
                }
                pHeader.Uncover();
            }
        }
Exemple #2
0
        /// <summary>
        /// Chooses the header node with the least amount of nodes connected to it
        /// </summary>
        private DancingLinks.HeaderNode ChooseHeaderNode()
        {
            DancingLinks.HeaderNode node   = (DancingLinks.HeaderNode)_links.Root.Right;
            DancingLinks.HeaderNode retval = node;

            while (node != _links.Root)
            {
                if (node.Count < retval.Count)
                {
                    retval = node;
                }
                node = (DancingLinks.HeaderNode)node.Right;
            }

            return(retval);
        }