示例#1
0
 public DancingLinksNode(DancingLinksHeader header, int row)
 {
     Header = header;
     Left   = this;
     Right  = this;
     Up     = this;
     Down   = this;
     RowID  = row;
 }
示例#2
0
        public DancingLinks(int[][] matrix)
        {
            _control = new DancingLinksHeader
            {
                Header = null,
                Up     = null,
                Down   = null
            };
            for (int c = 0; c < matrix[0].Length; c++)
            {
                var header = new DancingLinksHeader
                {
                    Left  = _control.Left,
                    Right = _control
                };
                header.Left.Right = header;
                header.Right.Left = header;
            }

            for (int i = 0; i < matrix.Length; i++)
            {
                DancingLinksHeader header = _control.Right as DancingLinksHeader;
                DancingLinksNode   last   = null;

                for (int j = 0; j < matrix[i].Length; j++, header = header.Right as DancingLinksHeader)
                {
                    if (matrix[i][j] == 0)
                    {
                        continue;
                    }

                    var tmp = new DancingLinksNode(header, i);

                    if (last != null)
                    {
                        tmp.Left       = last;
                        tmp.Right      = last.Right;
                        tmp.Left.Right = tmp;
                        tmp.Right.Left = tmp;
                    }
                    tmp.Up      = header.Up;
                    tmp.Down    = header;
                    tmp.Up.Down = tmp;
                    tmp.Down.Up = tmp;

                    header.Count++;
                    last = tmp;
                }
            }
        }
示例#3
0
        private DancingLinksHeader GetNextColumn()
        {
            DancingLinksHeader header = null;
            int min = int.MaxValue;

            for (var tmp = _control.Right as DancingLinksHeader; tmp != _control; tmp = tmp.Right as DancingLinksHeader)
            {
                if (min > tmp.Count)
                {
                    min    = tmp.Count;
                    header = tmp;
                }
            }

            return(header);
        }
示例#4
0
        private bool Search(List <int> solution)
        {
            if (_control.Right == _control)
            {
                var args = new SolutionFoundEventArgs(solution);

                SolutionFound(this, args);

                return(args.Terminate);
            }

            DancingLinksHeader header = GetNextColumn();

            Cover(header);
            for (DancingLinksNode node = header.Down; node != header; node = node.Down)
            {
                solution.Add(node.RowID);
                for (DancingLinksNode tmp = node.Right; tmp != node; tmp = tmp.Right)
                {
                    Cover(tmp);
                }

                if (Search(solution))
                {
                    return(true);
                }

                solution.RemoveAt(solution.Count - 1);
                for (DancingLinksNode tmp = node.Right; tmp != node; tmp = tmp.Right)
                {
                    Uncover(tmp);
                }
            }
            Uncover(header);

            return(false);
        }