private void button1_Click(object sender, EventArgs e) { int[,] nums = new int[9, 9]; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) nums[i, j] = int.Parse(controls[i, j].Text == string.Empty ? "0" : controls[i, j].Text); var startTime = DateTime.Now; var maze = new SudokuSolver.Maze(nums); try { IList<StepInfo> steps = null; nums = new SudokuSolver.SudokuSolver(maze).SolveAndGetSteps(out steps); if (Logger != null) ShowLog(steps); var timeElapsed = DateTime.Now.Subtract(startTime); updateUI(nums); timeElapsedLbl.Text = timeElapsed.TotalMilliseconds + " ms."; } catch (Exception ex) { MessageBox.Show("logic error :( "); } }
public static bool validate(Maze maze) { foreach (var cell in maze.GetCells()) { if (cell.Value == 0) return false; if (maze.GetColumn(cell.Column).Count(x => x.Value == cell.Value) != 1) return false; if (maze.GetRow(cell.Row).Count(x => x.Value == cell.Value) != 1) return false; if (maze.GetSquare(cell.Row, cell.Column).Count(x => x.Value == cell.Value) != 1) return false; } return true; }
static void Main(string[] args) { int[,] array = new int[,] { { 8, 9, 0, 4, 0, 0, 0, 1, 0 }, //easy { 6, 5, 0, 0, 0, 0, 0, 0, 0 }, { 2, 7, 1, 0, 0, 0, 0, 4, 0 }, { 0, 6, 8, 1, 0, 7, 2, 9, 0 }, { 0, 0, 0, 8, 0, 3, 0, 0, 0 }, { 0, 1, 9, 2, 0, 6, 4, 3, 0 }, { 0, 8, 0, 0, 0, 0, 1, 7, 4 }, { 0, 0, 0, 0, 0, 0, 0, 5, 6 }, { 0, 4, 0, 0, 0, 5, 0, 0, 2 } }; array = new int[,] { { 4, 0, 7, 0, 0, 0, 0, 0, 0 },//medium { 0, 0, 2, 0, 4, 0, 1, 0, 0 }, { 0, 1, 0, 6, 5, 0, 0, 4, 8 }, { 9, 3, 0, 0, 6, 7, 0, 0, 0 }, { 0, 0, 8, 0, 0, 0, 6, 0, 0 }, { 0, 0, 0, 9, 2, 0, 0, 5, 7 }, { 7, 2, 0, 0, 1, 5, 0, 3, 0 }, { 0, 0, 3, 0, 9, 0, 5, 0, 0 }, { 0, 0, 0, 0, 0, 0, 4, 0, 1 } }; array = new int[,] { { 7, 0, 5, 3, 0, 0, 1, 0, 0 }, //medium { 8, 4, 0, 2, 1, 0, 0, 0, 3 }, { 0, 1, 0, 7, 0, 0, 0, 0, 4 }, { 1, 0, 7, 0, 5, 0, 0, 0, 0 }, { 0, 9, 0, 0, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 9, 0, 3, 0, 6 }, { 5, 0, 0, 0, 0, 1, 0, 9, 0 }, { 9, 0, 0, 0, 7, 2, 0, 3, 1 }, { 0, 0, 1, 0, 0, 6, 7, 0, 5 } }; array = new int[,] { { 0, 0, 0, 8, 0, 0, 0, 0, 4 },//medium { 9, 0, 0, 0, 1, 5, 6, 7, 0 }, { 1, 0, 0, 0, 6, 9, 0, 0, 0 }, { 0, 9, 0, 0, 5, 0, 0, 4, 1 }, { 0, 0, 7, 0, 9, 0, 8, 0, 0 }, { 6, 3, 0, 0, 4, 0, 0, 9, 0 }, { 0, 0, 0, 9, 8, 0, 0, 0, 6 }, { 0, 6, 9, 5, 7, 0, 0, 0, 8 }, { 3, 0, 0, 0, 0, 6, 0, 0, 0 } }; array = new int[,] { { 1, 3, 0, 7, 8, 0, 5, 0, 2 },//medium { 0, 0, 7, 3, 6, 0, 0, 0, 0 }, { 4, 0, 5, 0, 0, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0, 0, 0, 0, 0 }, { 9, 6, 0, 0, 4, 0, 0, 5, 3 }, { 0, 0, 0, 0, 0, 0, 0, 0, 4 }, { 0, 0, 0, 0, 0, 0, 4, 0, 5 }, { 0, 0, 0, 0, 1, 7, 6, 0, 0 }, { 3, 0, 1, 0, 5, 9, 0, 8, 7 } }; array = new int[,] { { 0, 0, 0, 0, 0, 8, 0, 4, 5 },//hard { 0, 1, 4, 0, 2, 0, 0, 0, 7 }, { 0, 8, 0, 0, 5, 1, 0, 0, 0 }, { 0, 2, 9, 0, 0, 0, 0, 0, 0 }, { 6, 0, 0, 0, 8, 0, 0, 0, 2 }, { 0, 0, 0, 0, 0, 0, 4, 9, 0 }, { 0, 0, 0, 3, 7, 0, 0, 6, 0 }, { 4, 0, 0, 0, 9, 0, 8, 3, 0 }, { 9, 6, 0, 8, 0, 0, 0, 0, 0 } }; array = new int[,] { { 0, 0, 9, 0, 0, 6, 0, 0, 2 },//evil { 0, 0, 0, 0, 5, 4, 0, 3, 0 }, { 0, 5, 0, 0, 0, 0, 9, 0, 0 }, { 0, 0, 3, 0, 2, 0, 0, 0, 5 }, { 0, 9, 5, 0, 0, 0, 3, 2, 0 }, { 6, 0, 0, 0, 8, 0, 1, 0, 0 }, { 0, 0, 6, 0, 0, 0, 0, 8, 0 }, { 0, 4, 0, 6, 1, 0, 0, 0, 0 }, { 3, 0, 0, 7, 0, 0, 2, 0, 0 } }; var m = new Maze(array); var s = m.GetSquare(0, 3); new SudokuSolver(m).Solve(); // Console.ReadLine(); }
public SudokuSolver(Maze maze) { StepInfo.ResetCounter(); this.maze = maze; }