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

Valid Sudoku

时间:2015-07-09 00:13:06      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

Question:

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.

Solution:

 1 bool row_valid(vector<char> row)
 2 {
 3     bool result=true;
 4     int flag[9]={0};
 5     for(int i=0;i<row.size();i++)
 6     {
 7         if(row[i]==1) flag[0]++;
 8         else if(row[i]==2) flag[1]++;
 9         else if(row[i]==3) flag[2]++;
10         else if(row[i]==4) flag[3]++;
11         else if(row[i]==5) flag[4]++;
12         else if(row[i]==6) flag[5]++;
13         else if(row[i]==7) flag[6]++;
14         else if(row[i]==8) flag[7]++;
15         else if(row[i]==9) flag[8]++;
16     }
17     for(int i=0;i<9;i++)
18     {
19         if(flag[i]>1)
20         {
21             result=false;
22             break;
23         }
24     }
25     return result;
26 }
27 bool is_valid(vector< vector<char> > &rows)
28 {
29     bool result=true;
30     int n=rows.size();
31     for(int i=0;i<n;i++)
32     {
33         if(row_valid(rows[i])==false)
34         {
35             result =false;
36             break;
37         }
38     }
39     return result;
40 }
41 class Solution {
42 public:
43     bool isValidSudoku(vector<vector<char>>& board) {
44     vector<int> p;
45     vector< vector<char> > col_board;
46     vector< vector<char> > grid_board;
47     for(int i=0;i<9;i++)
48     {
49         vector<char> p;
50         for(int j=0;j<9;j++)
51         {
52             p.push_back(board[j][i]);
53             //col_board[i].push_back(board[j][i]);
54         }
55         col_board.push_back(p);
56 
57     }
58     //col_board.erase(col_board.begin(),col_board.begin()+board.size());
59     vector<char> temp;
60     for(int i=0;i<9;i++)
61     {
62         for(int j=0;j<9;j++)
63             temp.push_back(board[i][j]);
64     }
65     vector<char> v0,v1,v2,v3,v4,v5,v6,v7,v8;
66     for(int i=0;i<temp.size();i++)
67     {
68         int row=floor(double(i)/9)+1;
69         int col=i-9*floor(double(i)/9)+1;
70         int k=3*floor(double(row-1)/3)+floor(double(col-1)/3);
71 
72         switch(k)
73         {
74             case 0:v0.push_back(temp[i]);break;
75             case 1:v1.push_back(temp[i]);break;
76             case 2:v2.push_back(temp[i]);break;
77             case 3:v3.push_back(temp[i]);break;
78             case 4:v4.push_back(temp[i]);break;
79             case 5:v5.push_back(temp[i]);break;
80             case 6:v6.push_back(temp[i]);break;
81             case 7:v7.push_back(temp[i]);break;
82             case 8:v8.push_back(temp[i]);break;
83         }
84         //grid_board[k].push_back(temp[i]);
85     }
86     grid_board.push_back(v0);grid_board.push_back(v1);grid_board.push_back(v2);
87     grid_board.push_back(v3);grid_board.push_back(v4);grid_board.push_back(v5);
88     grid_board.push_back(v6);grid_board.push_back(v7);grid_board.push_back(v8);
89     //grid_board.erase(grid_board.begin(),grid_board.begin()+board.size());
90 
91     if(is_valid(board) && is_valid(col_board) && is_valid(grid_board))
92         return true;
93     else return false;
94         
95     }
96 };

技术分享

Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/riden/p/4631548.html

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