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.

C++:
01 class Solution {
02 public:
03     bool isValidSudoku(vector<vector<char> > &board) {
04         // Start typing your C/C++ solution below
05         // DO NOT write int main() function
06         vector<set<char>> Row(9);
07         vector<set<char>> Col(9);
08         vector<set<char>> Cub(9);
09         for(int i=0;i<9;i++)
10         {
11             for(int j=0;j<9;j++)
12             {
13                 char c=board[i][j];
14                 int p=i/3;
15                 int q=j/3;
16                 int cnt=p*3+q;
17                 if(c!=‘.’)
18                 {
19                     if((Row[i].find(c)==Row[i].end())
20                     &&(Col[j].find(c)==Col[j].end())
21                     &&(Cub[cnt].find(c)==Cub[cnt].end()))
22                     {
23                         Row[i].insert(c);
24                         Col[j].insert(c);
25                         Cub[cnt].insert(c);
26                     }
27                     else
28                         return false;
29                 }
30             }
31         }
32         return true;
33     }
34 };
Advertisements
This entry was posted in Array and linked list, Backtrack. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s