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

[LeetCode] Valid Sudoku

时间:2014-08-26 13:06:46      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   strong   for   ar   art   

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.

方法:不必把空格都填好,只需检测给出的已经填好的数是否在行中、列中、小区域中重复,so easy,并不是检测它是否能填满哦

方法1:

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {  
        // Note: The Solution object is instantiated only once.   
        vector<int> row(9,0);
        vector<int> col(9,0);
        vector<int> tmp(9,0); 
        for(int i = 0; i < 9; i++)  
        {  
            row = tmp;
            col = tmp;
            for(int j = 0; j < 9; j++)  
            {  
                if(board[i][j] != .)  
                {  
                    if(row[board[i][j]-1] > 0)return false;  
                    else row[board[i][j]-1]++;  
                }  
                if(board[j][i] != .)  
                {  
                    if(col[board[j][i]-1] > 0)return false;  
                    else col[board[j][i]-1]++;  
                }  
            }  
        }  

        for(int i = 0; i < 9; i+=3)  
            for(int j = 0; j < 9; j+=3)  
            {  
                row = tmp;
                for(int a = 0; a < 3; a++)  
                    for(int b= 0; b < 3; b++)  
                        if(board[i+a][j+b] != .)  
                        {  
                            if(row[board[i+a][j+b]-1]>0)return false;  
                            else row[board[i+a][j+b]-1]++;  
                        }  
            }  
            return true;  
    }  
};

方法2:

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(board.size()<9 || board[0].size()<9)
           return false;
           
    vector<bool> tmp(9,false);
        vector<vector<bool> > r(9,tmp);
        vector<vector<bool> > c(r);
    vector<vector<bool> > b(r);
        
        for(int i=0; i<9; i++)
           for(int j=0; j<9; j++)
           {
               if(board[i][j]==.)
                 continue;
               int a = (i/3)*3 + j/3;
               int number = board[i][j]-1;
               if(r[i][number] || c[j][number] || b[a][number])
                   return false;
               r[i][number] = c[j][number] = b[a][number] = 1;
           }
        return true;
    }
};

方法3:

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        int row,row2;
        int col,col2;
        int sqr,sqr2;
        int a,b,c;

        for(int i=0;i<9;i++){
            row=0;row2 = 0;
            col=0;col2 = 0;
            sqr = 0;sqr2 =0;
            for(int j=0;j<9;j++){
               a = board[i][j] - 0;
               b = board[j][i] -  0;
               c = board[3*(i%3)+j/3][3*(i/3)+j%3] - 0;
               if(a>0) row2 ^= 1<< a;
               if(b>0) col2 ^= 1<< b;
               if(c>0) sqr2 ^= 1<< c;

               if(row2 < row || col2<col || sqr2<sqr)
                   return false;
               row = row2;
               col = col2;
               sqr = sqr2;
            }
        }
        return true;
    }
};

 

[LeetCode] Valid Sudoku

标签:style   blog   http   color   io   strong   for   ar   art   

原文地址:http://www.cnblogs.com/Xylophone/p/3936872.html

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