Example #1
0
        internal virtual void Initialize(int width, int height)
        {
            this.width  = width;
            this.height = height;
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    dancer.AddColumn(new Pentomino.Point(x, y));
                }
            }
            int pieceBase = dancer.GetNumberColumns();

            foreach (Pentomino.Piece p in pieces)
            {
                dancer.AddColumn(p);
            }
            bool[] row = new bool[dancer.GetNumberColumns()];
            for (int idx = 0; idx < pieces.Count; ++idx)
            {
                Pentomino.Piece piece = pieces[idx];
                row[idx + pieceBase] = true;
                GenerateRows(dancer, piece, width, height, false, row, idx == 0);
                if (piece.GetFlippable())
                {
                    GenerateRows(dancer, piece, width, height, true, row, idx == 0);
                }
                row[idx + pieceBase] = false;
            }
            printer = new Pentomino.SolutionPrinter(width, height);
        }
Example #2
0
        private DancingLinks <Sudoku.ColumnName> MakeModel()
        {
            DancingLinks <Sudoku.ColumnName> model = new DancingLinks <Sudoku.ColumnName>();

            // create all of the columns constraints
            for (int x = 0; x < size; ++x)
            {
                for (int num = 1; num <= size; ++num)
                {
                    model.AddColumn(new Sudoku.ColumnConstraint(num, x));
                }
            }
            // create all of the row constraints
            for (int y = 0; y < size; ++y)
            {
                for (int num = 1; num <= size; ++num)
                {
                    model.AddColumn(new Sudoku.RowConstraint(num, y));
                }
            }
            // create the square constraints
            for (int x_1 = 0; x_1 < squareYSize; ++x_1)
            {
                for (int y_1 = 0; y_1 < squareXSize; ++y_1)
                {
                    for (int num = 1; num <= size; ++num)
                    {
                        model.AddColumn(new Sudoku.SquareConstraint(num, x_1, y_1));
                    }
                }
            }
            // create the cell constraints
            for (int x_2 = 0; x_2 < size; ++x_2)
            {
                for (int y_1 = 0; y_1 < size; ++y_1)
                {
                    model.AddColumn(new Sudoku.CellConstraint(x_2, y_1));
                }
            }
            bool[] rowValues = new bool[size * size * 4];
            for (int x_3 = 0; x_3 < size; ++x_3)
            {
                for (int y_1 = 0; y_1 < size; ++y_1)
                {
                    if (board[y_1][x_3] == -1)
                    {
                        // try each possible value in the cell
                        for (int num = 1; num <= size; ++num)
                        {
                            model.AddRow(GenerateRow(rowValues, x_3, y_1, num));
                        }
                    }
                    else
                    {
                        // put the given cell in place
                        model.AddRow(GenerateRow(rowValues, x_3, y_1, board[y_1][x_3]));
                    }
                }
            }
            return(model);
        }