Skip to content

Commit 3e41530

Browse files
committed
test: 37 solution
py, c++, go, java
1 parent 54e684e commit 3e41530

File tree

5 files changed

+168
-57
lines changed

5 files changed

+168
-57
lines changed

problems/problems_37/Solution.cpp

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,74 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
33

4-
54
using namespace std;
65
using json = nlohmann::json;
76

7+
constexpr int N = 9;
8+
89
class Solution {
910
public:
10-
void solveSudoku(vector<vector<char>>& board) {
11-
11+
void solveSudoku(vector<vector<char>> &board) {
12+
array<int, N> rows{}, cols{}, boxes{};
13+
for (int i = 0; i < N; ++i) {
14+
for (int j = 0; j < N; ++j) {
15+
if (board[i][j] != '.') {
16+
rows[i] |= 1 << (board[i][j] - '1');
17+
cols[j] |= 1 << (board[i][j] - '1');
18+
boxes[(i / 3) * 3 + (j / 3)] |= 1 << (board[i][j] - '1');
19+
}
20+
}
1221
}
22+
auto backtrack = [&board, &rows, &cols, &boxes](this auto &&backtrack,
23+
int i, int j) -> bool {
24+
if (i == N)
25+
return true;
26+
if (j == N)
27+
return backtrack(i + 1, 0);
28+
if (board[i][j] != '.')
29+
return backtrack(i, j + 1);
30+
for (char c = '1'; c <= '9'; ++c) {
31+
int v = 1 << (c - '1');
32+
int boxI = (i / 3) * 3 + (j / 3);
33+
if ((rows[i] & v) || (cols[j] & v) || (boxes[boxI] & v))
34+
continue;
35+
board[i][j] = c;
36+
rows[i] |= v;
37+
cols[j] |= v;
38+
boxes[boxI] |= v;
39+
if (backtrack(i, j + 1))
40+
return true;
41+
board[i][j] = '.';
42+
rows[i] &= ~v;
43+
cols[j] &= ~v;
44+
boxes[boxI] &= ~v;
45+
}
46+
return false;
47+
};
48+
49+
backtrack(0, 0);
50+
}
1351
};
1452

1553
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
54+
vector<string> inputArray;
55+
size_t pos = input_json_values.find('\n');
56+
while (pos != string::npos) {
57+
inputArray.push_back(input_json_values.substr(0, pos));
58+
input_json_values = input_json_values.substr(pos + 1);
59+
pos = input_json_values.find('\n');
60+
}
61+
inputArray.push_back(input_json_values);
2462

25-
Solution solution;
26-
vector<vector<string>> board_str = json::parse(inputArray.at(0));
27-
auto board = vector<vector<char>>(board_str.size(), vector<char>(board_str[0].size()));
28-
for (size_t i = 0; i < board.size(); ++i) {
29-
for (size_t j = 0; j < board[i].size(); ++j) {
30-
board[i][j] = board_str[i][j][0];
31-
}
32-
}
33-
solution.solveSudoku(board);
34-
return board;
63+
Solution solution;
64+
vector<vector<string>> board_str = json::parse(inputArray.at(0));
65+
auto board =
66+
vector<vector<char>>(board_str.size(), vector<char>(board_str[0].size()));
67+
for (size_t i = 0; i < board.size(); ++i) {
68+
for (size_t j = 0; j < board[i].size(); ++j) {
69+
board[i][j] = board_str[i][j][0];
70+
}
71+
}
72+
solution.solveSudoku(board);
73+
return board;
3574
}

problems/problems_37/Solution.java

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,72 @@
11
package problems.problems_37;
22

3-
import java.util.Stack;
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
46

5-
public class Solution {
7+
8+
public class Solution extends BaseSolution {
9+
private static final int N = 9;
610
public void solveSudoku(char[][] board) {
7-
Stack<Integer> empty = new Stack<>();
8-
for (int i = 0; i <= 8; i++) {
9-
for (int j = 0; j <= 8; j++) {
11+
int[] rows = new int[N];
12+
int[] cols = new int[N];
13+
int[] boxes = new int[N];
14+
for (int i = 0; i < N; i++) {
15+
for (int j = 0; j < N; j++) {
1016
if (board[i][j] == '.') {
11-
empty.push(9 * i + j);
17+
continue;
1218
}
19+
int v = 1 << (board[i][j] - '1');
20+
rows[i] |= v;
21+
cols[j] |= v;
22+
boxes[(i / 3) * 3 + (j / 3)] |= v;
1323
}
1424
}
15-
solve(board, empty);
25+
backtrack(board, rows, cols, boxes, 0, 0);
1626
}
1727

18-
private boolean solve(char[][] board, Stack<Integer> empty) {
19-
if (empty.isEmpty()) return true;
20-
int firstValue = empty.peek();
21-
int row = firstValue / 9, col = firstValue % 9;
22-
for (int k = 1; k <= 9; k++) {
23-
if (isSafe(board, row, col, (char) (k + '0'))) {
24-
board[row][col] = (char) (k + '0');
25-
empty.pop();
26-
if (solve(board, empty)) return true;
27-
board[row][col] = '.';
28-
empty.push(firstValue);
28+
private boolean backtrack(char[][] board, int[] rows, int[] cols, int[] boxes, int i, int j) {
29+
if (i == N) {
30+
return true;
31+
}
32+
if (j == N) {
33+
return backtrack(board, rows, cols, boxes, i + 1, 0);
34+
}
35+
if (board[i][j] != '.') {
36+
return backtrack(board, rows, cols, boxes, i, j + 1);
37+
}
38+
for (int c = 0; c < N; ++c) {
39+
int v = 1 << c;
40+
int boxId = (i / 3) * 3 + (j / 3);
41+
if (((rows[i] & v) != 0) || ((cols[j] & v) != 0) || ((boxes[boxId] & v) != 0)) {
42+
continue;
43+
}
44+
char cr = (char)(c + '1');
45+
board[i][j] = cr;
46+
rows[i] |= v;
47+
cols[j] |= v;
48+
boxes[boxId] |= v;
49+
if (backtrack(board, rows, cols, boxes, i, j + 1)) {
50+
return true;
2951
}
52+
rows[i] &= ~v;
53+
cols[j] &= ~v;
54+
boxes[boxId] &= ~v;
3055
}
56+
board[i][j] = '.';
3157
return false;
3258
}
3359

34-
private boolean isSafe(char[][] board, int i, int j, char ch) {
35-
for (int k = 0; k < 9; k++) {
36-
if (board[k][j] == ch) return false;
37-
if (board[i][k] == ch) return false;
38-
}
39-
int starti = 3 * (i / 3), startj = 3 * (j / 3);
40-
for (int k = starti; k < starti + 3; k++) {
41-
for (int l = startj; l < startj + 3; l++) {
42-
if (board[k][l] == ch) return false;
60+
@Override
61+
public Object solve(String[] inputJsonValues) {
62+
char[][] board = jsonArrayToChar2DArray(inputJsonValues[0]);
63+
solveSudoku(board);
64+
String[][] boardStr = new String[N][N];
65+
for (int i = 0; i < N; i++) {
66+
for (int j = 0; j < N; j++) {
67+
boardStr[i][j] = String.valueOf(board[i][j]);
4368
}
4469
}
45-
return true;
70+
return JSON.toJSON(boardStr);
4671
}
47-
}
72+
}

problems/problems_37/solution.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,51 @@ import (
66
"strings"
77
)
88

9-
func solveSudoku(board [][]byte) {
10-
9+
const N = 9
10+
11+
func solveSudoku(board [][]byte) {
12+
rows, cols, boxes := make([]int, N), make([]int, N), make([]int, N)
13+
for i := range N {
14+
for j := range N {
15+
if board[i][j] != '.' {
16+
v := 1 << (board[i][j] - '1')
17+
rows[i] |= v
18+
cols[j] |= v
19+
boxes[i/3*3+j/3] |= v
20+
}
21+
}
22+
}
23+
24+
var backtrack func(i, j int) bool
25+
backtrack = func(i, j int) bool {
26+
if i == N {
27+
return true
28+
}
29+
if j == N {
30+
return backtrack(i+1, 0)
31+
}
32+
if board[i][j] != '.' {
33+
return backtrack(i, j+1)
34+
}
35+
for c := range N {
36+
v := 1 << c
37+
if rows[i]&v == 0 && cols[j]&v == 0 && boxes[i/3*3+j/3]&v == 0 {
38+
rows[i] |= v
39+
cols[j] |= v
40+
boxes[i/3*3+j/3] |= v
41+
board[i][j] = byte(c + '1')
42+
if backtrack(i, j+1) {
43+
return true
44+
}
45+
rows[i] &^= v
46+
cols[j] &^= v
47+
boxes[i/3*3+j/3] &^= v
48+
board[i][j] = '.'
49+
}
50+
}
51+
return false
52+
}
53+
backtrack(0, 0)
1154
}
1255

1356
func Solve(inputJsonValues string) any {
@@ -30,10 +73,13 @@ func Solve(inputJsonValues string) any {
3073
return byteArrToStrArr(board)
3174
}
3275

33-
func byteArrToStrArr(arr [][]byte) []string {
34-
ans := make([]string, len(arr))
76+
func byteArrToStrArr(arr [][]byte) [][]string {
77+
ans := make([][]string, len(arr))
3578
for i, b := range arr {
36-
ans[i] = string(b)
79+
ans[i] = make([]string, len(b))
80+
for j, v := range b {
81+
ans[i][j] = string(v)
82+
}
3783
}
3884
return ans
3985
}

problems/problems_37/testcase

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
["[[\"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\"]]"]
2-
[[["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"]]]
1+
["[[\"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\"]]", "[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\"9\",\".\",\".\",\"1\",\".\",\".\",\"3\",\".\"],[\".\",\".\",\"6\",\".\",\"2\",\".\",\"7\",\".\",\".\"],[\".\",\".\",\".\",\"3\",\".\",\"4\",\".\",\".\",\".\"],[\"2\",\"1\",\".\",\".\",\".\",\".\",\".\",\"9\",\"8\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\"2\",\"5\",\".\",\"6\",\"4\",\".\",\".\"],[\".\",\"8\",\".\",\".\",\".\",\".\",\".\",\"1\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]"]
2+
[[["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"]], [["7","2","1","8","5","3","9","4","6"],["4","9","5","6","1","7","8","3","2"],["8","3","6","4","2","9","7","5","1"],["9","6","7","3","8","4","1","2","5"],["2","1","4","7","6","5","3","9","8"],["3","5","8","2","9","1","6","7","4"],["1","7","2","5","3","6","4","8","9"],["6","8","3","9","4","2","5","1","7"],["5","4","9","1","7","8","2","6","3"]]]

problems/problems_37/testcase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self):
1818
["4", "2", "6", "8", "5", "3", "7", "9", "1"], ["7", "1", "3", "9", "2", "4", "8", "5", "6"],
1919
["9", "6", "1", "5", "3", "7", "2", "8", "4"], ["2", "8", "7", "4", "1", "9", "6", "3", "5"],
2020
["3", "4", "5", "2", "8", "6", "1", "7", "9"]]))
21+
self.testcases.append(case(Input=[[".",".",".",".",".",".",".",".","."],[".","9",".",".","1",".",".","3","."],[".",".","6",".","2",".","7",".","."],[".",".",".","3",".","4",".",".","."],["2","1",".",".",".",".",".","9","8"],[".",".",".",".",".",".",".",".","."],[".",".","2","5",".","6","4",".","."],[".","8",".",".",".",".",".","1","."],[".",".",".",".",".",".",".",".","."]], Output=[["7","2","1","8","5","3","9","4","6"],["4","9","5","6","1","7","8","3","2"],["8","3","6","4","2","9","7","5","1"],["9","6","7","3","8","4","1","2","5"],["2","1","4","7","6","5","3","9","8"],["3","5","8","2","9","1","6","7","4"],["1","7","2","5","3","6","4","8","9"],["6","8","3","9","4","2","5","1","7"],["5","4","9","1","7","8","2","6","3"]]))
2122

2223
def get_testcases(self):
2324
return self.testcases

0 commit comments

Comments
 (0)