static void Main(string[] args) { string easy = "619030040270061008000047621486302079000014580031009060005720806320106057160400030"; string easy1 = "000000307048720010005014008250003876000000000671200039500840700030052980106000000"; string easy2 = "409120030050903100000040089910004007020010060600800051780030000004709010090065408"; string easy3 = "060001020970820400035004001604000018007000200820000605700900130002067094040500080"; string medium = "000300000001080070980007610007900040204000308060008900075800031020060700000004000"; string hard = "000004800590200004100800090001000508070000040309000100030001002200008067004900000"; string expert = "060090020100000000070001800015300002000604000800005430003400050000000009050070060"; string diabolic = "000000000000003085001020000000507000004000100090000000500000073002010000000040009"; string diabolic2 = "900040000000010200370000005000000090001000400000705000000020100580300000000000000"; string zen = "000000000000000000000000000000000000000010000000000000000000000000000000000000000"; string test1 = "305420810487901506029056374850793041613208957074065280241309065508670192096512408"; // Singles string test2 = "002030008000008000031020000060050270010000050204060031000080605000000013005310400"; // Hidden SIngles string test3 = "090300001000080046000000800405060030003275600060010904001000000580020000200007060"; // Unsolveable Sudoku sudoku = new Sudoku(zen); sudoku.PrintSuduko(); if (!sudoku.Solve()) { sudoku.SolveBasic(); } sudoku.PrintSuduko(); Console.ReadLine(); }
private void Button_Solve_Click(object sender, EventArgs e) { int[,] sudokuValues = new int[9, 9]; for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { sudokuValues[x, y] = blocks[(int)Math.Floor(x / 3.0), (int)Math.Floor(y / 3.0)] .GetCellValue(x % 3, y % 3); } } Sudoku solver = new Sudoku(); for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { solver.SetValue(x, y, sudokuValues[x, y]); } } bool solved = solver.Solve(); if (solved) { for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { blocks[(int)Math.Floor(x / 3.0), (int)Math.Floor(y / 3.0)] .SetCellValue(x % 3, y % 3, solver.GetValue(x, y)); } } } }
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { int[][] board = ExtractBoard(); Sudoku.Solve(ref board); UpdateGridView(board); }
static void Main(string[] args) { Console.Title = "Sudoku Solver"; while (true) { // get filepath from input and read it's contents Console.WriteLine("Enter path to text file:"); string filepath = Console.ReadLine(); string[] lines = FileReader.ReadFromFile(filepath); if (lines != null) { // print contents from file Console.WriteLine("\nContents read from file:\n"); foreach (string line in lines) { Console.WriteLine(line); } Console.WriteLine(); // parse text to sudoku grid int[,] numbers = SudokuParser.ParseText(lines); if (numbers != null) { Console.WriteLine("succesfully parsed sudoku\n"); Sudoku sudoku = new Sudoku(); for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { sudoku.SetValue(x, y, numbers[x, y]); } } sudoku.PrintSudoku(); Console.WriteLine("solving sudoku..."); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); bool solved = sudoku.Solve(); stopwatch.Stop(); if (solved) { Console.WriteLine($"solution found in {stopwatch.ElapsedMilliseconds} milliseconds"); } else { Console.WriteLine($"no solution found in {stopwatch.ElapsedMilliseconds} milliseconds"); } sudoku.PrintSudoku(); } } } }
/// <summary> /// attempts to solve the sudoku /// </summary> /// <returns>true if successful, false otherwise</returns> public bool Solve() { if (!IsSudokuValid()) { return(false); } List <SudokuCell>[] rows = new List <SudokuCell> [9]; List <SudokuCell>[] columns = new List <SudokuCell> [9]; List <SudokuCell>[] blocks = new List <SudokuCell> [9]; List <SudokuCell> fixedCells = new List <SudokuCell>(); List <SudokuCell> emptyCells = new List <SudokuCell>(); for (int i = 0; i < 9; i++) { rows[i] = new List <SudokuCell>(); columns[i] = new List <SudokuCell>(); blocks[i] = new List <SudokuCell>(); } // add cells to rows, columns, blocks and to empty or fixed cells for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { SudokuCell cell = new SudokuCell(i, j, GetValue(i, j)); rows[cell.Row].Add(cell); columns[cell.Column].Add(cell); blocks[cell.Block].Add(cell); if (cell.Value == 0) { emptyCells.Add(cell); } else { fixedCells.Add(cell); } } } // eliminate fixed cell values from possible values in cells on same rows, columns or blocks fixedCells.ForEach(fixedCell => { UpdatePossibleValues(rows[fixedCell.Row], fixedCell.Value); UpdatePossibleValues(columns[fixedCell.Column], fixedCell.Value); UpdatePossibleValues(blocks[fixedCell.Block], fixedCell.Value); }); // keep updating cells untill sudoku is solved or no more updates are possible while (emptyCells.Count > 0) { int updates = 0; // get all cells from empty cells that now have a value emptyCells.ForEach(cell => { if (cell.Value != 0) { fixedCells.Add(cell); SetValue(cell.Row, cell.Column, cell.Value); updates += UpdatePossibleValues(rows[cell.Row], cell.Value); updates += UpdatePossibleValues(columns[cell.Column], cell.Value); updates += UpdatePossibleValues(blocks[cell.Block], cell.Value); } }); emptyCells.RemoveAll(c => fixedCells.Contains(c)); // fill in missing values in rows, columns and blocks for (int i = 0; i < 9; i++) { updates += FillInMissingValues(rows[i]); updates += FillInMissingValues(columns[i]); updates += FillInMissingValues(blocks[i]); } if (updates == 0) { break; } } // check if sudoku is solved if (emptyCells.Count == 0) { return(IsSudokuValid()); } else { // find the empty cell with the least number of possible values emptyCells.ForEach(cell => { if (cell.Value != 0) { fixedCells.Add(cell); SetValue(cell.Row, cell.Column, cell.Value); } }); emptyCells.RemoveAll(c => fixedCells.Contains(c)); emptyCells.Sort((a, b) => a.PossibleValues.Count - b.PossibleValues.Count); SudokuCell cell = emptyCells.First(); for (int i = 1; i <= 9; i++) { if (cell.PossibleValues.Contains(i)) { Sudoku sudoku = new Sudoku(); for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { sudoku.SetValue(x, y, GetValue(x, y)); } } sudoku.SetValue(cell.Row, cell.Column, i); if (sudoku.Solve()) { for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { SetValue(x, y, sudoku.GetValue(x, y)); } } return(true); } } } return(false); } }