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 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> /// 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); }