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

Valid Sudoku

时间:2014-09-03 19:39:47      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   strong   ar   for   art   

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 ‘.‘.

bubuko.com,布布扣 

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.

思路:检查每一行、每一列以及9个3*3的子块是否满足要求即可。

 1 class Solution {
 2 public:
 3     bool isValidSudoku( vector<vector<char> > &board ) {
 4         short rowMark[9] = {0}, colMark[9] = {0}, squareMark[9] = {0};
 5         for( int i = 0; i < 9; ++i ) {
 6             for( int j = 0; j < 9; ++j ) {
 7                 if( board[i][j] == . ) { continue; }
 8                 short bitflag = 1 << board[i][j]-1;
 9                 if( ( rowMark[i] | colMark[j] | squareMark[i/3*3+j/3] ) & bitflag ) { return false; }
10                 rowMark[i] |= bitflag;
11                 colMark[j] |= bitflag;
12                 squareMark[i/3*3+j/3] |= bitflag;
13             }
14         }
15         return true;
16     }
17 };

 

Valid Sudoku

标签:style   blog   http   color   io   strong   ar   for   art   

原文地址:http://www.cnblogs.com/moderate-fish/p/3952427.html

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