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

Valid Sudoku

时间:2015-03-10 10:18:29      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:

  简单的数学运算

我的代码:

技术分享
public class Solution {
    public boolean isValidSudoku(char[][] board) {
        if(board == null || board.length == 0 || board[0].length == 0)  return true;
        int row = board.length;
        int col = board[0].length;
        for(int i = 0; i < row; i++)
        {
            for(int j = 0; j < col; j++)
            {
                char c = board[i][j];
                if(c == ‘.‘)    continue;
                else
                {
                    //test row
                    for(int k = 0; k < j; k++)
                    {
                        if(board[i][k] == c)    return false;
                    }
                    //test col
                    for(int k = 0; k < i; k++)
                    {
                        if(board[k][j] == c)  return false;
                    }
                    //test box
                    int boxRow = i/3;
                    int boxCol = j/3;
                    for(int m = boxRow * 3; m < boxRow * 3 + 3; m++)
                    {
                        for(int n = boxCol * 3; n < boxCol * 3 + 3; n++)
                        {
                            if(board[m][n] == ‘.‘ || (m == i && n == j))  continue;
                            else
                            {
                                if(board[m][n] == c)    return false;
                            }
                        }
                    }
                }
            }
            
        }
        return true;
    }
}
View Code

他人代码:

技术分享
public boolean isValidSudoku(char[][] board) {
        // Start typing your Java solution below
        // DO NOT write main() function
       
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                if(board[i][j]==‘.‘)
                    continue;
                char tmp = board[i][j];
                board[i][j] = ‘C‘;
                boolean tr = isValid(board, i, j, tmp);
                board[i][j] = tmp;
                if(tr == false)
                    return tr;
            }
        }
        return true;
    }
   
    public boolean isValid(char[][] board, int x, int y, char tmp){
        for(int i=0; i<9; i++){
            if(board[x][i] == tmp || board[i][y] == tmp)
                return false;
        }
       
        int start_x = x/3;
        int start_y = y/3;
        for(int i=0; i<9; i++){
            int cur_x = start_x*3 + i/3;
            int cur_y = start_y*3 + i%3;
            if(board[cur_x][cur_y] == tmp)
                return false;
        }
        return true;
    }
View Code

学习之处:

  • 测试了好多次才过 不能忍
  • 他人的代码用9 进行测试,简单易行可以学习下

 

Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/sunshisonghit/p/4325145.html

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