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

LeetCode(36)Valid Sudoku

时间:2015-08-25 21:46:23      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:leetcode   character   

题目

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.

分析

这是一道关于数独游戏的题目,首先要了解数独游戏的规则:
技术分享
所以,对于该题目,有些空格中是’.’ 字符,我们只需要考虑当前状态下是否满足数独即可。
也就是说,我们要按行、按列,按每个3*3宫格,检验三次。

AC代码

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        if (board.empty())
            return false;

        //数独游戏符合9宫格,也就是为一个9*9的矩阵
        int size = board.size();

        //根据数独游戏的规则,需要进行三次检验(按行、按列、按照3*3块)
        //利用哈希的思想,记录每个关键字的出现次数,若是次数>1,则返回false
        vector<int> count;
        for (int i = 0; i < size; ++i)
        {
            //每行开始前,将记录次数的vector清零,元素1~9分别对应下标0~8,对应vector中值为该元素的出现次数
            count.assign(9, 0);
            for (int j = 0; j < size; j++)
            {
                if (board[i][j] != ‘.‘)
                {
                    int pos = board[i][j] - ‘1‘;                    
                    if (count[pos] > 0)
                        return false;
                    else
                        ++count[pos];
                }
                else
                    continue;

            }//for
        }//for

        //同理,按列检验
        for (int j = 0; j < size; j++)
        {
            count.assign(9, 0);
            for (int i = 0; i < size; i++)
            {
                if (board[i][j] != ‘.‘)
                {
                    int pos = board[i][j] - ‘1‘;

                    if (count[pos] > 0)
                        return false;
                    else
                        ++count[pos];;
                }
                else
                    continue;
            }//for
        }//for

        //按3*3小块检验
        for (int i = 0; i < size; i += 3)
        {           
            for (int j = 0; j < size; j += 3)
            {
                count.assign(9, 0);
                //每个块又是一个3*3的矩阵
                for (int row = i; row < i + 3;row++)
                for (int col = j; col < j + 3; col++)
                {
                    if (board[row][col] != ‘.‘)
                    {
                        int pos = board[row][col] - ‘1‘;
                        if (count[pos] > 0)
                            return false;
                        else
                            ++count[pos];;
                    }
                    else
                        continue;
                }
            }//for
        }//for

        return true;
    } 
};

GitHub测试程序源码

版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode(36)Valid Sudoku

标签:leetcode   character   

原文地址:http://blog.csdn.net/fly_yr/article/details/47983109

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