public static bool useBacktrackingAlgorithm(State state, int i, int j) { // state.showField(); depth++; if (j == 9)// { j = 0; i++; if (i == 9) { return true; } } if (state.field[i, j] != 0) return (goodGuessSol(state, i, j + 1)); foreach (var val in state.candidates[i, j]) if (checkValid(i, j, val, state) == true) { state.field[i, j] = val; if (goodGuessSol(state, i, j + 1) == true) return true; } state.field[i, j] = 0; return false; }
public static bool checkValid(int i, int j, int val, State tmp) { for (int k = 0; k < 9; ++k) if ((val == tmp.field[k, j])) return false; for (int k = 0; k < 9; ++k) if ((val == tmp.field[i, k])) return false; int boxRowOffset = (i / 3) * 3; int boxColOffset = (j / 3) * 3; for (int k = 0; k < 3; ++k) for (int m = 0; m < 3; ++m) if ((val == tmp.field[boxRowOffset + k, boxColOffset + m])) return false; return true; }