/// <summary> /// Apply singles strategy to supplied grid as much as possible /// </summary> /// <param name="grid"></param> public static void Singles(Grid grid) { int row = 0; int column = 0; while (row < grid.CellsInRow) { while (column < grid.CellsInRow) { if (grid.cells[row, column] == 0) { IList candidates = grid.Possibilities(row, column); if (candidates.Count == 1) { grid.cells[row, column] = (int)candidates[0]; // Restart search from top left row = 0; column = 0; } else { column++; } } else { column++; } } row++; } }
private void CheckCandidates() { for (int row = 0; row < grid.CellsInRow; row++) { for (int column = 0; column < grid.CellsInRow; column++) { int cellControlId = (1000 * (row + 1)) + column; CellControl cntlFoc = (CellControl)GetControl(cellControlId); if (cntlFoc.CellValue == 0) { IList candidates = grid.Possibilities(row, column); for (int i = 1; i <= 9; i++) //foreach (int candidate in candidates) { if (!candidates.Contains(i)) { cntlFoc.RemoveCandidate(i); } } cntlFoc.HighlightCandidate(_Settings.Filter); if ((_Settings.Filter == 0) && (candidates.Count == 2)) { cntlFoc.Highlight = true; } cntlFoc.ShowCandidates = _Settings.ShowCandidates; } } } }
public static bool FindCandidateInCol(Grid grid, int row, int col, int Num) { for (int i = 0; i < grid.CellsInRow; i++) { if (grid.IsCellEmpty(i, col) && i != row && grid.Possibilities(i, col).Contains(Num)) { return(true); } } return(false); }
public static bool FindCandidateInBox(Grid grid, int row, int col, int Num) { int firstRow = (row / 3) * 3; int firstCol = (col / 3) * 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (grid.IsCellEmpty(firstRow + i, firstCol + j) && grid.Possibilities(firstRow + i, firstCol + j).Contains(Num) && !(firstRow + i != row || firstCol + j != col)) { return(true); } } } return(false); }
public void HiddenSingles(Grid grid) { // First try rows for (int row = 0; row < grid.CellsInRow; row++) { int[] hiddenSingles = new int[grid.CellsInRow]; for (int value = 0; value < grid.CellsInRow; value++) { hiddenSingles[value] = -1; } // Now get possibilities for all cells for (int column = 0; column < grid.CellsInRow; column++) { IList candidates = grid.Possibilities(row, column); foreach (int candidate in candidates) { if (hiddenSingles[candidate - 1] == -1) { hiddenSingles[candidate - 1] = column; } else { hiddenSingles[candidate - 1] = grid.CellsInRow; } } } for (int value = 0; value < grid.CellsInRow; value++) { if (hiddenSingles[value] >= 0 && hiddenSingles[value] < grid.CellsInRow) { // Got a hidden single grid.cells[row, hiddenSingles[value]] = value; } } } }
public static bool FindCandidateInBox(Grid grid, int row, int col, int Num) { int firstRow = (row / 3) * 3; int firstCol = (col / 3) * 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (grid.IsCellEmpty(firstRow + i, firstCol + j) && grid.Possibilities(firstRow + i, firstCol + j).Contains(Num) && !(firstRow + i != row || firstCol + j != col)) { return true; } } } return false; }
public static bool FindCandidateInCol(Grid grid, int row, int col, int Num) { for (int i = 0; i < grid.CellsInRow; i++) { if (grid.IsCellEmpty(i, col) && i != row && grid.Possibilities(i, col).Contains(Num)) { return true; } } return false; }
/// <summary> /// Find all Unique Candidates in grid /// and fill out the uniqueCandidates Array in the grid /// </summary> /// <param name="grid">Grid</param> /// <returns>the number of Unique Candidates</returns> public static int FindUniqueCandidates(Grid grid) { // Grid grid = (Grid)gridOrg.Clone(); grid.resetUniqueCandidates(); nodes = 0; int uniquesCand = 0; unsolvableCell = false; //find Unique Candidates in rows for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInRow(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } //find Unique Candidates in colums for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInCol(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } //find Unique Candidates in boxes for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInBox(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } else { if (candidates.Count == 1) { uniquesCand++; } else { unsolvableCell = true; } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (grid.uniqueCandidates[i, j] != 0) { nodes++; } } } nodes = nodes + (uniquesCand / 2); return nodes; }
/// <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> /// Find all Unique Candidates in grid /// and fill out the uniqueCandidates Array in the grid /// </summary> /// <param name="grid">Grid</param> /// <returns>the number of Unique Candidates</returns> public static int FindUniqueCandidates(Grid grid) { // Grid grid = (Grid)gridOrg.Clone(); grid.resetUniqueCandidates(); nodes = 0; int uniquesCand = 0; unsolvableCell = false; //find Unique Candidates in rows for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInRow(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } //find Unique Candidates in colums for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInCol(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } //find Unique Candidates in boxes for (int searchRow = 0; searchRow < grid.CellsInRow; searchRow++) { for (int searchCol = 0; searchCol < grid.CellsInRow; searchCol++) { if (!grid.IsCellEmpty(searchRow, searchCol)) { continue; } IList candidates = grid.Possibilities(searchRow, searchCol); IList candidates2 = new ArrayList(candidates); if (candidates.Count > 1) { for (int cand = 0; cand < candidates.Count; cand++) { if (FindCandidateInBox(grid, searchRow, searchCol, (int)candidates[cand])) { candidates2.Remove(candidates[cand]); } } } else { if (candidates.Count == 1) { uniquesCand++; } else { unsolvableCell = true; } } if (candidates2.Count == 1) { grid.uniqueCandidates[searchRow, searchCol] = (int)candidates2[0]; } } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (grid.uniqueCandidates[i, j] != 0) { nodes++; } } } nodes = nodes + (uniquesCand / 2); return(nodes); }
/// <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); }