码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Sudoku

时间:2014-06-04 20:21:51      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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 ‘.‘.

bubuko.com,布布扣

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.

bubuko.com,布布扣
class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        bool exist[10];
        //check row
        for(int i=0;i<9;i++)
        {
            for(int k=0;k<9;k++) exist[k+1]=false;;
            for(int j=0;j<9;j++)
            if(board[i][j]!=.)
            {
                 if(exist[board[i][j]-0]) return false;
                 exist[board[i][j]-0]=true;
            }
        }
        //check column
        for(int i=0;i<9;i++)
        {
            for(int k=0;k<9;k++) exist[k+1]=false;
            for(int j=0;j<9;j++)
            if(board[j][i]!=.)
            {
                 if(exist[board[j][i]-0]) return false;
                 exist[board[j][i]-0]=true;
            }
        }
        //check square
        for(int di=0;di<9;di+=3)
            for(int dj=0;dj<9;dj+=3)
            {
                for(int k=0;k<9;k++) exist[k+1]=false;
                for(int i=0;i<3;i++)
                    for(int j=0;j<3;j++)
                    {
                        char c=board[di+i][dj+j];
                        if(c!=.)
                        {
                            if(exist[c-0]) return false;
                            exist[c-0]=true;
                        }
                    }
            }
        return true;
    }
}; 
bubuko.com,布布扣

Valid Sudoku,布布扣,bubuko.com

Valid Sudoku

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/erictanghu/p/3759333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!