forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid_Sudoku.cpp
73 lines (64 loc) · 1.5 KB
/
Valid_Sudoku.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Author: Weixian Zhou, [email protected]
Date: Jul 27, 2012
Problem: Valid Sudoku
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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 '.'.
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
bool number[10];
for (int i = 0; i < 9; i++) {
memset(number, 0, sizeof(number));
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
if (number[board[i][j] - '0']) {
return false;
}
number[board[i][j] - '0'] = true;
}
}
}
for (int i = 0; i < 9; i++) {
memset(number, 0, sizeof(number));
for (int j = 0; j < 9; j++) {
if (board[j][i] != '.') {
if (number[board[j][i] - '0']) {
return false;
}
number[board[j][i] - '0'] = true;
}
}
}
for (int i = 0; i < 9; i++) {
memset(number, 0, sizeof(number));
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
int bx = i / 3 * 3, by = i % 3 * 3;
if (board[bx + x][by + y] != '.') {
if (number[board[bx + x][by + y] - '0']) {
return false;
}
number[board[bx + x][by + y] - '0'] = true;
}
}
}
}
return true;
}
};