Example #1
0
        static List <Node <bool> > DancingLinks(TorodialDoubleLinkList <bool> list)
        {
            List <Node <bool> > solutions = new List <Node <bool> >();
            ColumnNode <bool>   column    = list.H;

            return(Search(list, column, solutions));
        }
Example #2
0
        static List <Node <bool> > Search(TorodialDoubleLinkList <bool> list, ColumnNode <bool> column, List <Node <bool> > solutions)
        {
            if (list.H.Right == list.H)
            {
                foreach (Node <bool> result in solutions)
                {
                    Console.Write(result.ColumnNode.ID + "," + result.Index + " ");
                }
                Console.WriteLine();
                return(solutions);
            }
            else
            {
                column = getNextColumn(list);
                Cover(column);

                Node <bool> rowNode = column;

                while (rowNode.Down != column)
                {
                    rowNode = rowNode.Down;

                    solutions.Add(rowNode);

                    Node <bool> rightNode = rowNode;

                    while (rightNode.Right != rowNode)
                    {
                        rightNode = rightNode.Right;

                        Cover(rightNode);
                    }

                    List <Node <bool> > result = Search(list, column, solutions);

                    if (result != null)
                    {
                        return(result);
                    }

                    solutions.Remove(rowNode);
                    column = rowNode.ColumnNode;

                    Node <bool> leftNode = rowNode;

                    while (leftNode.Left != rowNode)
                    {
                        leftNode = leftNode.Left;

                        Uncover(leftNode);
                    }
                }

                Uncover(column);
            }

            return(null);
        }
Example #3
0
        static ColumnNode <bool> getNextColumn(TorodialDoubleLinkList <bool> list)
        {
            ColumnNode <bool> node       = list.H;
            ColumnNode <bool> chosenNode = null;

            while (node.Right != list.H)
            {
                node = (ColumnNode <bool>)node.Right;

                if (chosenNode == null || node.Size < chosenNode.Size)
                {
                    chosenNode = node;
                }
            }

            return(chosenNode);
        }