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

    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;
        }
    }; 

  • 相关阅读:
    Linux内核中的jiffies 以及时间的获取time
    linux2.6内核启动流程简述
    qt 显示控件 导致频闪
    V4L2应用程序框架
    linux 目标机 windows 图形界面ftp登录
    linux2.4内核启动流程简述及2410主频修改
    块设备驱动编写总结一(ZT)
    backlight misc驱动范例 及应用程序范例
    如何通过结构中的某个成员地址获取结构本身的指针???
    我是怎么招聘程序员的
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759333.html
Copyright © 2011-2022 走看看