leetcode problem 37 -- 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.


A sudoku puzzle...


 

 

思路:

  搜索加剪枝。

  首先设立3个辅助二维数组:rows,  columns, grids来维护目前的数独状态,rows[i][k](1<=i, k<=9)表示第i行是否占用了数字k,同理colums[i][k]表示第i列是否占用了数字k,grid[i][k]表示第i个格子是否占用了数字k,

  然后第一次读入二维数组,更新rows, columns和grids.

  第二次读二维数组,对每个为'.'的元素根据rows,columns和grid来枚举可能的值。如果没有可以枚举的值,直接剪枝返回。

 

代码如下:

  Runtime: 5 ms

static int columns[9][10];

static int rows[9][10];

static int grids[9][10];





int search(char *board[9], int startI, int startJ) {



    for(int i = startI; i < 9; ++i) {

        for(int j = i == startI ? startJ : 0; j < 9; ++j) {

            if (board[i][j] == '.') {

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

                    if (!rows[i][k] && !columns[j][k] && !grids[i/3 + j/3 *3][k]) {

                        rows[i][k] = 1;

                        columns[j][k] = 1; 

                           grids[i/3 + j/3 *3][k] = 1;



                        if (search(board, i, j+1) == 1) {

                            board[i][j] = k + '0';

                            return 1;

                        }

                        rows[i][k] = 0;

                        columns[j][k] = 0; 

                           grids[i/3 + j/3 *3][k] = 0;

                    }

                }

                return 0;

            }

        }

    }

    return 1;

}





void solveSudoku(char * board[9]) {

    memset(columns, 0, sizeof(columns));    

    memset(rows, 0, sizeof(rows));    

    memset(grids, 0, sizeof(grids));    



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

        for(int j = 0; j < 9; ++j) {

            if (board[i][j] != '.') {

                rows[i][board[i][j] - '0'] = 1;

                columns[j][board[i][j] - '0'] = 1;



                int tmp = i/3 + j/3 * 3; 

                grids[tmp][board[i][j] - '0'] = 1;

            }

        }

    }

    search(board, 0, 0);

}

 

你可能感兴趣的:(LeetCode)