LeetCode Online Judge 题目C# 练习 - Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

 1         public static void SudokuSolver(List<List<char>> board)

 2         {

 3             solver(board);

 4         }

 5 

 6         public static bool solver(List<List<char>> board)

 7         {

 8             for (int r = 0; r < 9; r++)

 9             {

10                 for (int c = 0; c < 9; c++)

11                 {

12                     if (board[r][c] == '.')

13                     {

14                         for (int k = 1; k <= 9; k++)

15                         {

16                             board[r][c] = Convert.ToChar('0' + k);

17                             if (isValid(board, r, c))

18                             {

19                                 if(solver(board))

20                                     return true;

21                             }

22                             board[r][c] = '.';

23                         }

24                         return false;

25                     }

26                 }

27             }

28             return true;

29         }

30 

31         public static bool isValid(List<List<char>> board, int r, int c)

32         {

33             //////////////CHECK ROW/////////////////////

34             bool[] Row = new bool[9];

35             for (int i = 0; i < 9; i++)

36             {

37                 if (board[r][i] >= '1' && board[r][i] <= '9')

38                 {

39                     if (Row[board[r][i] - '1'] == false)

40                         Row[board[r][i] - '1'] = true;

41                     else

42                         return false;

43                 }

44             }

45 

46             /////////////CHECK COLUMN//////////////////

47             bool[] Col = new bool[9];

48             for (int i = 0; i < 9; i++)

49             {

50                 if (board[i][c] >= '1' && board[i][c] <= '9')

51                 {

52                     if (Col[board[i][c] - '1'] == false)

53                         Col[board[i][c] - '1'] = true;

54                     else

55                         return false;

56                 }

57             }

58 

59             /////////////CHECK GRID///////////////////

60             bool[] Grid = new bool[9];

61             // r / 3 * 3 = beginning row number of that grid

62             // c / 3 * 3 = beginning column number of that grid

63             for (int i = (r / 3) * 3; i < (r / 3) * 3 + 3; i++)

64             {

65                 for (int j = (c / 3) * 3; j < (c / 3) * 3 + 3; j++)

66                 {

67                     if (board[i][j] >= '1' && board[i][j] <= '9')

68                     {

69                         if (Grid[board[i][j] - '1'] == false)

70                             Grid[board[i][j] - '1'] = true;

71                         else

72                             return false;

73                     }

74                 }

75             }

76 

77             return true;

78         }

代码分析:

  递归,就是每个数字试,检查行列格子都Valid就递归到下一个,如果不Valid记得要把当前数字归空( '.' ),因为这这个格子有可能0~9都不Valid,需要回溯到前一个格子。DP有点不知道怎么弄。

  我也有个Follow Up, 如果要打印所有可能的解。该怎么写呢?

你可能感兴趣的:(LeetCode)