zoukankan      html  css  js  c++  java
  • leetcode[36]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.

    #define NUM 9
    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {
        map<char,int> bmap;
        map<char,int> bmap1;
        map<char,int> bmap2;
        for (int i=0;i<NUM;i++)
        {
            bmap.clear();
            bmap1.clear();
            for (int j=0;j<NUM;j++)
            {
                if (board[i][j]!='.')
                {
                    bmap[board[i][j]]++;
                    if(bmap[board[i][j]]>1)return false;
                }
                if (board[j][i]!='.')
                {
                    bmap1[board[j][i]]++;
                    if (bmap1[board[j][i]]>1)return false;
                }
            }
        }
        for (int a=0;a<NUM;a+=3)
        {
            for (int b=0;b<NUM;b+=3)
            {
                bmap2.clear();
                for (int ii=a;ii<a+3;ii++)
                {
                    for (int jj=b;jj<b+3;jj++)
                    {
                        if (board[ii][jj]!='.')
                        {
                            bmap2[board[ii][jj]]++;
                            if (bmap2[board[ii][jj]]>1)return false;
                        }
                    }
                }
            }
        }
        return true;
        }
    };
  • 相关阅读:
    HDOJ 1877
    POJ 2210
    HDOJ 1230(火星A+B)
    大数想减
    HDU 2115
    HDOJ 1234
    HDOJ 3784
    HDOJ3782(xxx定理)
    C# 使用 Stopwatch 测量代码运行时间
    SQL返回当前天是星期几
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4283595.html
Copyright © 2011-2022 走看看