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

leetcode Valid Sudoku

时间:2015-05-11 10:41:20      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

代码:

 1 #include<iostream>
 2 #include<vector>
 3 
 4 using namespace std;
 5 
 6 int length = 9;
 7 
 8 bool isValidSudoku(vector<vector<char>>& board) 
 9 {
10     int flag[9];
11     memset(flag, 0, sizeof(flag)*length);
12     //
13     for (int j = 0; j < length; j++)
14     {
15         for (int i = 0; i < length; i++)
16         {
17             if (board[i][j] != .)
18             {
19                 if (flag[board[i][j] - 1] == 1)
20                     return false;
21                 else
22                     flag[board[i][j] - 1] = 1;
23             }
24         }
25         memset(flag, 0, sizeof(flag)*length);
26     }
27     memset(flag, 0, sizeof(flag)*length);
28     for (int i = 0; i < length; i++)
29     {
30         for (int j = 0; j < length; j++)
31         {
32             if (board[i][j] != .)
33             {
34                 if (flag[board[i][j] - 1] == 1)
35                     return false;
36                 else
37                     flag[board[i][j] - 1] = 1;
38             }
39         }
40         memset(flag, 0, sizeof(flag)*length);
41     }
42     memset(flag, sizeof(flag), 0);
43     for (int i0 = 0; i0 < length; i0+=3)
44     {
45         for (int j0 = 0; j0 < length; j0+=3)
46         {
47             for (int i = i0; i < i+3; i++)
48             {
49                 for (int j = j0; j < j+3; j++)
50                 {
51                     if (board[i][j] != .)
52                     {
53                         if (flag[board[i][j] - 1] == 1)
54                             return false;
55                         else
56                             flag[board[i][j] - 1] = 1;
57                     }
58                 }
59             }
60             memset(flag, 0, sizeof(flag)*length);
61         }
62     }
63     return true;
64 }
65 
66 int main()
67 {
68     vector<vector<char>> a = 
69     {
70         { 5, 3, ., ., 7, ., ., ., . },
71         { 6, ., ., 1, 9, 5, ., ., . },
72         { ., 9, 8, ., ., ., ., 6, . },
73         { 8, ., ., ., 6, ., ., ., 3 },
74         { 4, ., ., 8, ., 3, ., ., 1 },
75         { 7, ., ., ., 2, ., ., ., 6 },
76         { ., 6, ., ., ., ., 2, 8, . },
77         { ., ., ., 4, 1, 9, ., ., 5 },
78         { ., ., ., ., 8, ., ., 7, 9 },
79     };
80     cout << isValidSudoku(a) << endl;
81     return 0;
82 }

 

leetcode Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/chaiwentao/p/4493685.html

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