-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path37.sudoku-solver.cpp
More file actions
145 lines (130 loc) · 4.93 KB
/
37.sudoku-solver.cpp
File metadata and controls
145 lines (130 loc) · 4.93 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Tag: Array, Hash Table, Backtracking, Matrix
// Time: O(N^(N^2))
// Space: O(N)
// Ref: -
// Note: -
// Video: https://youtu.be/XJ9Y4vUEplU
// Write a program to solve a Sudoku puzzle by filling the empty cells.
// A sudoku solution must satisfy all of the following rules:
//
// Each of the digits 1-9 must occur exactly once in each row.
// Each of the digits 1-9 must occur exactly once in each column.
// Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
//
// The '.' character indicates empty cells.
//
// Example 1:
//
//
// Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
// Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
// Explanation: The input board is shown above and the only valid solution is shown below:
//
//
//
//
// Constraints:
//
// board.length == 9
// board[i].length == 9
// board[i][j] is a digit or '.'.
// It is guaranteed that the input board has only one solution.
//
//
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
int n = board.size();
vector<int> row(n, 0), col(n, 0), sec(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '0';
int k = (i / 3) * 3 + (j / 3);
int bit = 1 << num;
row[i] |= bit;
col[j] |= bit;
sec[k] |= bit;
}
}
}
dfs(board, row, col, sec, 0, 0, n);
}
bool dfs(vector<vector<char>>& board,
vector<int>& row,
vector<int>& col,
vector<int>& sec,
int i, int j, int n) {
if (i >= n) {
return true;
} else if (j >= n) {
return dfs(board, row, col, sec, i + 1, 0, n);
} else if (board[i][j] != '.') {
return dfs(board, row, col, sec, i, j + 1, n);
} else {
int k = (i / 3) * 3 + (j / 3);
for (int num = 1; num <= 9; num++) {
int bit = 1 << num;
if ((row[i] & bit) || (col[j] & bit) || (sec[k] & bit)) {
continue;
}
board[i][j] = '0' + num;
row[i] |= bit;
col[j] |= bit;
sec[k] |= bit;
if (dfs(board, row, col, sec, i, j + 1, n)) {
return true;
}
row[i] ^= bit;
col[j] ^= bit;
sec[k] ^= bit;
board[i][j] = '.';
}
return false;
}
}
};
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
int n = board.size();
vector<vector<bool>> row(n, vector(n + 1, false));
vector<vector<bool>> col(n, vector(n + 1, false));
vector<vector<bool>> zone(n, vector(n + 1, false));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == '.') {
continue;
}
int num = board[i][j] - '0';
int k = i / 3 * 3 + j / 3;
row[i][num] = col[j][num] = zone[k][num] = true;
}
}
dfs(board, row, col, zone);
}
bool dfs(vector<vector<char>>& board, vector<vector<bool>>& row, vector<vector<bool>>& col, vector<vector<bool>>& zone) {
int n = board.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == '.') {
int k = i / 3 * 3 + j / 3;
for (int num = 1; num <= 9; num++) {
if (row[i][num] || col[j][num] || zone[k][num]) {
continue;
}
row[i][num] = col[j][num] = zone[k][num] = true;
board[i][j] = num + '0';
if (dfs(board, row, col, zone)) {
return true;
}
board[i][j] = '.';
row[i][num] = col[j][num] = zone[k][num] = false;
}
return false;
}
}
}
return true;
}
};