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)); }
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); }
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); }