36. Valid Sudoku

来源:互联网 发布:ubuntu装回win7 编辑:程序博客网 时间:2024/06/05 17:41

题目:Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character'.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

数独的特点:

所有数字必须是1-9之间。是一个9行9列的表。

(1)每行的数字不能重复

(2)每列的数字不能重复

(3)包含9个9宫格,每个9宫格中的数字也不能重复。


代码:

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {    int i=0,j=0;    int tmp1[9];    memset(tmp1,0,9*sizeof(int));        int tmp2[9];    memset(tmp2,0,9*sizeof(int));        int tmp3[9];    memset(tmp3,0,9*sizeof(int));        for(i=0;i<9;i++)    {        memset(tmp1,0,9*sizeof(int));        memset(tmp2,0,9*sizeof(int));                for(j=0;j<9;j++)        {            //第i行是否符合               if( board[i][j] != '.')             {                int sub1=board[i][j]-'0'-1;                //printf("The sub1 is %d\n",sub1);                if(tmp1[sub1]==0)                {                    tmp1[sub1]++;                }                else if(tmp1[sub1] >= 1)                {                    printf("The row\n");                    return false;                }            }                        //第i列是否符合            if( board[j][i] != '.')             {                int sub2=board[j][i]-'0'-1;                if(j==3)                    printf("The sub2 is %d, i is %d, j is %d\n",sub2,i,j);                if(tmp2[sub2]==0)                {                    tmp2[sub2]++;                }                else if(tmp2[sub2] >= 1)                {                    printf("The colum\n");                    return false;                }            }                                                //九宫格是否符合            if(i%3==0 && j%3==0)            {                memset(tmp3,0,9*sizeof(int));                for(int p=i;p<=i+2;p++ )                {                    for(int q=j;q<=j+2;q++)                    {                        if( board[p][q] != '.')                         {                            int sub3=board[p][q]-'0'-1;                            //printf("The sub3 is %d\n",sub3);                            if(tmp3[sub3]==0)                            {                                tmp3[sub3]++;                            }                            else if(tmp3[sub3] >= 1)                            {                                printf("The zoo\n");                                return false;                            }                        }                    }                }            }        }    }        return true;}
























0 0