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

    检查有数字的格子是否组成合法的数独。

    用row_mask[i][j]来记录第i行是否出现过数字j。

    bool isValidSudoku(vector<vector<char> > &board) {
        const int cnt = 9;
        bool row_mask[cnt][cnt] = {false};
        bool col_mask[cnt][cnt] = {false};
        bool area_mask[cnt][cnt] = {false};
        //check each rows and cols
        for(int r=0; r<board.size(); r++){
            for (int c=0; c<board[r].size(); c++){
                if (!isdigit(board[r][c])) continue;
                int idx =  board[r][c] - '0' - 1;
                    
                //check the rows
                if (row_mask[r][idx] == true){
                    return false;
                }
                row_mask[r][idx] = true;
                    
                //check the cols
                if (col_mask[c][idx] == true) {
                    return false;
                }
                col_mask[c][idx] = true;
                    
                //check the areas
                int area = (r/3) * 3 + (c/3);
                if (area_mask[area][idx] == true) {
                    return false;
                }
                area_mask[area][idx] = true;
            }
        }
            
        return true;
    }
  • 相关阅读:
    jsp数据交互二
    jsp数据交互(一)
    JQuery操作DOM
    事件和动画
    Jquery选择器
    Optional容器(jdk1.8)
    java常见集合笔记
    字符串内存占用图解
    单例设计模式
    代码块
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5245054.html
Copyright © 2011-2022 走看看