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

LeetCode之“散列表”:Valid Sudoku

时间:2015-06-20 17:04:20      阅读:111      评论: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.

  这道题解法暂没有比较巧妙的,所以下边程序所用方法就是Brute Force(暴力破解):

 1 class Solution {
 2 public:
 3     int charToInt(char c)
 4     {
 5         char str[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 6         int intStr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 7         for(int i = 0; i < 9; i++)
 8         {
 9             if(c == str[i])
10                 return intStr[i];
11         }
12         return -1;
13     }
14     
15     bool isValidSudoku(vector<vector<char>>& board) {
16         unordered_map<int, int> hashMap;
17         for(int i = 1; i < 10; i++)
18             hashMap[i] = 0;
19         // row by row
20         for(int i = 0; i < 9; i++)
21         {
22             for(int k = 0; k < 10; k++)
23                 hashMap[k] = 0;
24             for(int j = 0; j < 9; j++)
25             {
26                 int tmp = charToInt(board[i][j]);
27                 if(tmp != -1)
28                 {
29                     hashMap[tmp]++;
30                     if(hashMap[tmp] > 1)
31                         return false;
32                 }
33             }
34         }
35         // column by column
36         for(int j = 0; j < 9; j++)
37         {
38             for(int k = 1; k < 10; k++)
39                 hashMap[k] = 0;
40             for(int i = 0; i < 9; i++)
41             {
42                 int tmp = charToInt(board[i][j]);
43                 if(tmp != -1)
44                 {
45                     hashMap[tmp]++;
46                     if(hashMap[tmp] > 1)
47                         return false;
48                 }
49             }
50         }
51         // 3*3 boxes by 3*3 boxes
52         for(int i = 0; i < 9; i += 3)
53         {
54             for(int j = 0; j < 9; j += 3)
55             {
56                 for(int k = 0; k < 10; k++)
57                     hashMap[k] = 0;
58                 for(int m = i; m < i + 3; m++)
59                     for(int n = j; n < j + 3; n++)
60                     {
61                         int tmp = charToInt(board[m][n]);
62                         if(tmp != -1)
63                         {
64                             hashMap[tmp]++;
65                             if(hashMap[tmp] > 1)
66                                 return false;
67                         }
68                     }
69             }
70         }
71         
72         return true;
73     }
74 };

 

LeetCode之“散列表”:Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/xiehongfeng100/p/4590691.html

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