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

【leetcode】Valid Sudoku

时间:2015-05-18 16:00:43      阅读:102      评论: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.

 

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<vector<char>>& board) {
 4         bool used[9];
 5 
 6         for(int i=0;i<9;i++)
 7         {
 8             fill(used,used+9,false);
 9             for(int j=0;j<9;j++) //row
10             {
11                 if(check(board[i][j],used)==false)
12                     return false
13             }
14 
15             fill(used,used+9,false);
16             for(int j=0;j<9;j++) //col
17             {
18                 if(check(board[j][i],used)==false)
19                     return false;
20             }
21         }
22 
23         for(int r=0;r<3;r++)
24         {
25             for(int c=0;c<3;c++)
26             {
27                 fill(used,used+9,false);
28                 for(int i=r*3;i<r*3+3;i++)
29                 {
30                     for(int j=c*3;j<c*3+3;j++)
31                         if(check(board[i][j],used)==false)
32                             return false;
33                 }
34             }
35         }
36 
37         return true;
38     }
39 
40     bool check(char ch,bool used[9])
41     {
42         if(ch==.) return true;
43 
44         if(used[ch-1]) return false;
45 
46         return used[ch-1]=true;
47 
48     }
49 };

 

【leetcode】Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/jawiezhu/p/4511880.html

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