public static Grid Solve(Grid grid) { IList solutions = Solve((Grid)grid.Clone(), 1); if (solutions.Count == 0) { return null; } else { return (Grid)solutions[0]; } }
public static Grid Solve(Grid grid) { IList solutions = Solve((Grid)grid.Clone(), 1); if (solutions.Count == 0) { return(null); } else { return((Grid)solutions[0]); } }
public static bool ValidNumberPlace(Grid grid) { // Check only one solution exists IList solutions = Solve((Grid)grid.Clone(), 2); if (solutions.Count == 1) { return(true); } // Either unsolvable or multiple solutions return(false); }
public static bool ValidNumberPlace(Grid grid) { // Check only one solution exists IList solutions = Solve((Grid)grid.Clone(), 2); if (solutions.Count == 1) { return true; } // Either unsolvable or multiple solutions return false; }
/// <summary> /// rate a grid /// returns it as int /// the lower the harder /// </summary> /// <param name="grid">Grid</param> /// <returns></returns> public static int Rate(Grid gridOrg) { Grid grid = (Grid)gridOrg.Clone(); int rate = 0; rateAll = 0; // for(int trys = 0; trys<10;trys++) // { // grid = (Grid)gridOrg.Clone(); // rate = 0; while (grid.CountFilledCells() < (grid.CellsInRow * grid.CellsInRow)) { int cand = FindUniqueCandidates(grid); rate += cand; if (cand > 0) { int candidateIndex = random.Next(cand); int m = -1, i = 0, j = 0, candidateRow = 0, candidateCol = 0; for (i = 0; i < 9 && m < candidateIndex; i++) { for (j = 0; j < 9 && m < candidateIndex; j++) { if (grid.uniqueCandidates[i, j] != 0) { m++; if (m == candidateIndex) { candidateRow = i; candidateCol = j; } } } } grid.cells[candidateRow, candidateCol] = grid.uniqueCandidates[candidateRow, candidateCol]; } else { //rate = rate - ((grid.BlocksAcross * grid.BlocksAcross) - grid.CountFilledCells()); //no more unique cells -> difficult grid rate = grid.CountFilledCells(); break; } } // rateAll += rate; // MediaPortal.GUI.Library.Log.WriteFile(MediaPortal.GUI.Library.LogType.Log, "Soduko Rate end "+trys+" notes: {0}", rate); // } return(rate); }
public static Grid Generate(int blocksAcross) { // First generate a random completed grid that is valid Grid empty = new Grid(blocksAcross); Grid solution = Solve(empty); Grid puzzle = (Grid)solution.Clone(); return(Minimize(puzzle)); /* * Random random = new Random(DateTime.Now.Millisecond); * * IList removeCandidates = new ArrayList(); * for (int cellNumber = 1; cellNumber <= 81; cellNumber++) * { * removeCandidates.Add(cellNumber); * } * * while (removeCandidates.Count > 0) * { * int removeIndex = random.Next(removeCandidates.Count); * int row = removeIndex / puzzle.CellsInRow; * int column = removeIndex % puzzle.CellsInRow; * * if (puzzle.cells[row, column] != 0) * { * int value = puzzle.cells[row, column]; * puzzle.cells[row, column] = 0; * IList solutions = Solve((Grid)puzzle.Clone(), 2); * if (solutions.Count == 2 || solutions.Count == 0) * { * puzzle.cells[row, column] = value; * } * else * { * cellsRemoved; * } * } * } * * IList solns = Solve((Grid)puzzle.Clone(), 10); * return puzzle; */ }
public static bool CheckMinimality(Grid grid) { Grid temp = (Grid)grid.Clone(); for (int row = 0; row < temp.CellsInRow; row++) { for (int column = 0; column < temp.CellsInRow; column++) { if (temp.cells[row, column] != 0) { int value = temp.cells[row, column]; temp.cells[row, column] = 0; if (ValidNumberPlace(temp)) { return(false); } temp.cells[row, column] = value; } } } return(true); }
public static bool CheckMinimality(Grid grid) { Grid temp = (Grid)grid.Clone(); for (int row = 0; row < temp.CellsInRow; row++) { for (int column = 0; column < temp.CellsInRow; column++) { if (temp.cells[row, column] != 0) { int value = temp.cells[row, column]; temp.cells[row, column] = 0; if (ValidNumberPlace(temp)) { return false; } temp.cells[row, column] = value; } } } return true; }
/// <summary> /// Solve given sudoku grid and solutions upto maximum number specified /// </summary> /// <param name="g"></param> /// <returns></returns> public static IList Solve(Grid grid, int maxSolutions) { ArrayList solutions = new ArrayList(); // If grid is solved then return as solution if (grid.CountFilledCells() == (grid.CellsInRow * grid.CellsInRow)) { solutions.Add(grid.Clone()); return(solutions); } // Solve singles //Singles(grid); // Choose unsolved cell int leastCandidatesRow = -1; int leastCandidatesColumn = -1; IList leastCandidates = null; for (int row = 0; row < grid.CellsInRow; row++) { for (int column = 0; column < grid.CellsInRow; column++) { if (grid.cells[row, column] == 0) { IList candidates = grid.Possibilities(row, column); // If cell has no possible value then grid is not solvable so quit now if (candidates.Count == 0) { return(solutions); } else if (leastCandidates == null || leastCandidates.Count > candidates.Count) { leastCandidatesRow = row; leastCandidatesColumn = column; leastCandidates = candidates; } } } } // For all candidates of unsolved cell if (leastCandidates != null) { while (leastCandidates.Count > 0) { // Set candidate int candidateIndex = random.Next(leastCandidates.Count); grid.cells[leastCandidatesRow, leastCandidatesColumn] = (int)leastCandidates[candidateIndex]; leastCandidates.RemoveAt(candidateIndex); Grid nextLevelGrid = (Grid)grid.Clone(); IList nextLevelSolutions = Solve(nextLevelGrid, maxSolutions); solutions.AddRange(nextLevelSolutions); // Trim number of solutions so we don't exceed maximum required if (solutions.Count > maxSolutions) { solutions.RemoveRange(0, solutions.Count - maxSolutions); } if (solutions.Count == maxSolutions) { return(solutions); } } } return(solutions); }
/// <summary> /// rate a grid /// returns it as int /// the lower the harder /// </summary> /// <param name="grid">Grid</param> /// <returns></returns> public static int Rate(Grid gridOrg) { Grid grid = (Grid)gridOrg.Clone(); int rate = 0; rateAll = 0; // for(int trys = 0; trys<10;trys++) // { // grid = (Grid)gridOrg.Clone(); // rate = 0; while (grid.CountFilledCells() < (grid.CellsInRow * grid.CellsInRow)) { int cand = FindUniqueCandidates(grid); rate += cand; if (cand > 0) { int candidateIndex = random.Next(cand); int m = -1, i = 0, j = 0, candidateRow = 0, candidateCol = 0; for (i = 0; i < 9 && m < candidateIndex; i++) { for (j = 0; j < 9 && m < candidateIndex; j++) { if (grid.uniqueCandidates[i, j] != 0) { m++; if (m == candidateIndex) { candidateRow = i; candidateCol = j; } } } } grid.cells[candidateRow, candidateCol] = grid.uniqueCandidates[candidateRow, candidateCol]; } else { //rate = rate - ((grid.BlocksAcross * grid.BlocksAcross) - grid.CountFilledCells()); //no more unique cells -> difficult grid rate = grid.CountFilledCells(); break; } } // rateAll += rate; // MediaPortal.GUI.Library.Log.WriteFile(MediaPortal.GUI.Library.LogType.Log, "Soduko Rate end "+trys+" notes: {0}", rate); // } return rate; }
/// <summary> /// Solve given sudoku grid and solutions upto maximum number specified /// </summary> /// <param name="g"></param> /// <returns></returns> public static IList Solve(Grid grid, int maxSolutions) { ArrayList solutions = new ArrayList(); // If grid is solved then return as solution if (grid.CountFilledCells() == (grid.CellsInRow * grid.CellsInRow)) { solutions.Add(grid.Clone()); return solutions; } // Solve singles //Singles(grid); // Choose unsolved cell int leastCandidatesRow = -1; int leastCandidatesColumn = -1; IList leastCandidates = null; for (int row = 0; row < grid.CellsInRow; row++) { for (int column = 0; column < grid.CellsInRow; column++) { if (grid.cells[row, column] == 0) { IList candidates = grid.Possibilities(row, column); // If cell has no possible value then grid is not solvable so quit now if (candidates.Count == 0) { return solutions; } else if (leastCandidates == null || leastCandidates.Count > candidates.Count) { leastCandidatesRow = row; leastCandidatesColumn = column; leastCandidates = candidates; } } } } // For all candidates of unsolved cell if (leastCandidates != null) { while (leastCandidates.Count > 0) { // Set candidate int candidateIndex = random.Next(leastCandidates.Count); grid.cells[leastCandidatesRow, leastCandidatesColumn] = (int)leastCandidates[candidateIndex]; leastCandidates.RemoveAt(candidateIndex); Grid nextLevelGrid = (Grid)grid.Clone(); IList nextLevelSolutions = Solve(nextLevelGrid, maxSolutions); solutions.AddRange(nextLevelSolutions); // Trim number of solutions so we don't exceed maximum required if (solutions.Count > maxSolutions) { solutions.RemoveRange(0, solutions.Count - maxSolutions); } if (solutions.Count == maxSolutions) { return solutions; } } } return solutions; }