Esempio n. 1
0
        public void Solve()
        {
            DLX solver = new DLX();

            foreach (SudokuMove move in SudokuMove.AllMoves()) {
                bool given = (_givens[move.C, move.R] == move.N + 1);

                solver.AddRow(move, given);
            }

            ArrayList answer = solver.Search();

            foreach (object r in answer) {
                SudokuMove move = r as SudokuMove;
                _solution[move.C, move.R] = move.N + 1;
            }
        }
Esempio n. 2
0
        public void Solve()
        {
            DLX solver = new DLX();

            foreach (SudokuMove move in SudokuMove.AllMoves()) {
                bool required = _givens[move.X, move.Y] == move.N + 1;
                solver.AddRow(move, required);
            }

            ArrayList answer = solver.Search();

            if (answer != null) {
                foreach (object r in answer) {
                    SudokuMove move = r as SudokuMove;
                    _solution[move.X, move.Y] = move.N + 1;
                }
            }
        }
Esempio n. 3
0
        public void Solve()
        {
            DLX solver = new DLX(); // one column per cell for leaving, one for arriving; and one special cell for start; one special cell for end

            foreach (ChessMove move in ChessMove.AllKnightsMoves()) {
                move.Mark(solver);
            }

            ArrayList answer = solver.Search();

            if (answer != null) {
                Dictionary<int, ChessMove> bysource = new Dictionary<int, ChessMove>();
                Dictionary<int, ChessMove> bydest = new Dictionary<int, ChessMove>();

                Console.Clear();
                foreach (object r in answer) {
                    ChessMove move = r as ChessMove;

                    bysource[move.C1] = move;
                    bydest[move.C2] = move;
                }

                int start = ChessMove.OffTheBoard;
                int cell = start;

                List<ChessMove> moves = new List<ChessMove>();

                while (true) {
                    ChessMove move = bysource[cell];
                    moves.Add(move);
                    cell = move.C2;

                    if (cell == start) {
                        break;
                    }
                }

                Console.WriteLine("Moves in cycle:");
                Console.WriteLine(moves.Count);
                Console.ReadKey();

                _solution = moves;
            }
        }
Esempio n. 4
0
        public IEnumerable<int> SolveAll()
        {
            DLX solver = new DLX();

            foreach (SudokuMove move in SudokuMove.AllMoves()) {
                bool required = _givens[move.X, move.Y] == move.N + 1;
                solver.AddRow(move, required);
            }

            foreach (ArrayList answer in solver.EnumerateSolutions()) {
                foreach (object r in answer) {
                    SudokuMove move = r as SudokuMove;
                    _solution[move.X, move.Y] = move.N + 1;
                }

                yield return 1; // hack
            }

            yield break;
        }
Esempio n. 5
0
            public void Mark(DLX solver)
            {
                // mark the cell as being used
                solver.Mark((0 * 5 * 81) + (Board * 81) + (Block * 9) + (3 * R) + C);

                // mark the column as containing n
                solver.Mark((1 * 5 * 81) + Board * 81 + 27 * (Block % 3) + 9 * C + N);

                // mark the row as containing n
                solver.Mark((2 * 5 * 81) + Board * 81 + 27 * (Block / 3) + 9 * R + N);

                // mark the block as containing n
                solver.Mark((3 * 5 * 81) + 9 * (Board * 9 + Block) + N);

                if ((Board == 0 && Block == 8) || (Board == 1 && Block == 6) || (Board == 3 && Block == 2) || (Board == 4 && Block == 0)) {
                    // also mark the middle board

                    int MiddleBlock = 8 - Block;
                    int MiddleBoard = 2;

                    // mark the column as containing n
                    solver.Mark((1 * 5 * 81) + MiddleBoard * 81 + 27 * (MiddleBlock % 3) + 9 * C + N);

                    // mark the row as containing n
                    solver.Mark((2 * 5 * 81) + MiddleBoard * 81 + 27 * (MiddleBlock / 3) + 9 * R + N);
                }
            }
Esempio n. 6
0
 public void Mark(DLX solver)
 {
     solver.Mark(C1).Mark(C2 + 65);
 }
Esempio n. 7
0
            public void Mark(DLX solver)
            {
                // add a row for this number in this row and column
                int block = (R / 3) + 3 * (C / 3); // relying on truncation, of course

                // mark the cell as being used
                solver.Mark(0 * 81 + 9 * R + C);

                // mark the column as containing n
                solver.Mark(1 * 81 + 9 * C + N);

                // mark the row as containing n
                solver.Mark(2 * 81 + 9 * R + N);

                // mark the block as containing n
                solver.Mark(3 * 81 + 9 * block + N);
            }