public void FillNextpuzzle(int i, int j) { if (puzzle[i, j] != 0) { if (i == SIZE && j == SIZE) { success = true; recordSolution(); count++; if (count > 1) { throw new PuzzleCompleteException(); } } else { int nexti = j == SIZE ? i + 1 : i; int nextj = j == SIZE ? 0 : j + 1; FillNextpuzzle(nexti, nextj); return; } return; } var fillList = SudokuFounctionLibrary.GenerateFillList(); fillList.ForEach(delegate(int item) { puzzle[i, j] = item; if (FillSuccess(i, j)) { if (i == SIZE && j == SIZE) { success = true; count++; recordSolution(); if (count > 1) { throw new PuzzleCompleteException(); } } else { int nexti = j == SIZE ? i + 1 : i; int nextj = j == SIZE ? 0 : j + 1; FillNextpuzzle(nexti, nextj); } } }); puzzle[i, j] = 0; }
/// <summary> /// 填充数独的下一个格子 /// </summary> /// <param name="i">格子位置:第i行</param> /// <param name="j">格子位置:第j列</param> public void FillNextGrid(int i, int j) { var fillList = SudokuFounctionLibrary.GenerateFillList(); fillList.ForEach(delegate(int item) { grid[i, j] = item; if (SudokuFounctionLibrary.FillSuccess(grid, i, j)) { if (i == LAST && j == LAST) { PrintResult(); return; } else { int nexti = j == LAST ? i + 1 : i; int nextj = j == LAST ? 0 : j + 1; FillNextGrid(nexti, nextj); } } }); }