首页 > 其他 > 详细

36. Valid Sudoku

时间:2017-07-11 00:11:32      阅读:308      评论:0      收藏:0      [点我收藏+]

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[i][j]表示第i行的数字j有没有出现过,col 和box同理

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int row[10][10] = {0}, col[10][10] = {0}, box[10][10] = {0};
          for(int i = 0; i < board.size(); i++)
          {
              for(int j = 0; j < board[i].size(); j++)
              {
                  if(board[i][j] != .)
                  {
                      int x = board[i][j] - 0 - 1;
                      int k = i/3 + j/3;
                      if(row[i][x] || col[j][x] || box[k][x])
                          return false;
                      row[i][x] = col[j][x] = box[k][x] = 1;
                  }
              }
          } 
          return true;
    }
};

36. Valid Sudoku

原文:http://www.cnblogs.com/Alruddy/p/7148349.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!