Skip to content

Commit 3eb71bc

Browse files
author
orrym
committed
Merge remote-tracking branch 'upstream/master'
2 parents 8f4df6b + ff76cbc commit 3eb71bc

97 files changed

Lines changed: 3472 additions & 102 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.algorithms.sudoku;
2+
3+
import java.util.stream.IntStream;
4+
5+
public class BacktrackingAlgorithm {
6+
7+
private static final int BOARD_SIZE = 9;
8+
private static final int SUBSECTION_SIZE = 3;
9+
private static final int BOARD_START_INDEX = 0;
10+
11+
private static final int NO_VALUE = 0;
12+
private static final int MIN_VALUE = 1;
13+
private static final int MAX_VALUE = 9;
14+
15+
private static int[][] board = {
16+
{8, 0, 0, 0, 0, 0, 0, 0, 0},
17+
{0, 0, 3, 6, 0, 0, 0, 0, 0},
18+
{0, 7, 0, 0, 9, 0, 2, 0, 0},
19+
{0, 5, 0, 0, 0, 7, 0, 0, 0},
20+
{0, 0, 0, 0, 4, 5, 7, 0, 0},
21+
{0, 0, 0, 1, 0, 0, 0, 3, 0},
22+
{0, 0, 1, 0, 0, 0, 0, 6, 8},
23+
{0, 0, 8, 5, 0, 0, 0, 1, 0},
24+
{0, 9, 0, 0, 0, 0, 4, 0, 0}
25+
};
26+
27+
public static void main(String[] args) {
28+
BacktrackingAlgorithm solver = new BacktrackingAlgorithm();
29+
solver.solve(board);
30+
solver.printBoard();
31+
}
32+
33+
private void printBoard() {
34+
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
35+
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
36+
System.out.print(board[row][column] + " ");
37+
}
38+
System.out.println();
39+
}
40+
}
41+
42+
private boolean solve(int[][] board) {
43+
for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) {
44+
for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) {
45+
if (board[r][c] == NO_VALUE) {
46+
for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
47+
board[r][c] = k;
48+
if (isValid(board, r, c) && solve(board)) {
49+
return true;
50+
}
51+
board[r][c] = NO_VALUE;
52+
}
53+
return false;
54+
}
55+
}
56+
}
57+
return true;
58+
}
59+
60+
private boolean isValid(int[][] board, int r, int c) {
61+
return rowConstraint(board, r) &&
62+
columnConstraint(board, c) &&
63+
subsectionConstraint(board, r, c);
64+
}
65+
66+
private boolean subsectionConstraint(int[][] board, int r, int c) {
67+
boolean[] constraint = new boolean[BOARD_SIZE];
68+
int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE;
69+
int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
70+
71+
int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE;
72+
int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
73+
74+
for (int i = subsectionRowStart; i < subsectionRowEnd; i++) {
75+
for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) {
76+
if (!checkConstraint(board, i, constraint, j)) return false;
77+
}
78+
}
79+
return true;
80+
}
81+
82+
private boolean columnConstraint(int[][] board, int c) {
83+
boolean[] constraint = new boolean[BOARD_SIZE];
84+
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
85+
.allMatch(i -> checkConstraint(board, i, constraint, c));
86+
}
87+
88+
private boolean rowConstraint(int[][] board, int r) {
89+
boolean[] constraint = new boolean[BOARD_SIZE];
90+
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
91+
.allMatch(i -> checkConstraint(board, r, constraint, i));
92+
}
93+
94+
private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) {
95+
if (board[r][c] != NO_VALUE) {
96+
if (!constraint[board[r][c] - 1]) {
97+
constraint[board[r][c] - 1] = true;
98+
} else {
99+
return false;
100+
}
101+
}
102+
return true;
103+
}
104+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.algorithms.sudoku;
2+
3+
class ColumnNode extends DancingNode {
4+
int size;
5+
String name;
6+
7+
ColumnNode(String n) {
8+
super();
9+
size = 0;
10+
name = n;
11+
C = this;
12+
}
13+
14+
void cover() {
15+
unlinkLR();
16+
for (DancingNode i = this.D; i != this; i = i.D) {
17+
for (DancingNode j = i.R; j != i; j = j.R) {
18+
j.unlinkUD();
19+
j.C.size--;
20+
}
21+
}
22+
}
23+
24+
void uncover() {
25+
for (DancingNode i = this.U; i != this; i = i.U) {
26+
for (DancingNode j = i.L; j != i; j = j.L) {
27+
j.C.size++;
28+
j.relinkUD();
29+
}
30+
}
31+
relinkLR();
32+
}
33+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.baeldung.algorithms.sudoku;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
public class DancingLinks {
8+
9+
private ColumnNode header;
10+
private List<DancingNode> answer;
11+
12+
private void search(int k) {
13+
if (header.R == header) {
14+
handleSolution(answer);
15+
} else {
16+
ColumnNode c = selectColumnNodeHeuristic();
17+
c.cover();
18+
19+
for (DancingNode r = c.D; r != c; r = r.D) {
20+
answer.add(r);
21+
22+
for (DancingNode j = r.R; j != r; j = j.R) {
23+
j.C.cover();
24+
}
25+
26+
search(k + 1);
27+
28+
r = answer.remove(answer.size() - 1);
29+
c = r.C;
30+
31+
for (DancingNode j = r.L; j != r; j = j.L) {
32+
j.C.uncover();
33+
}
34+
}
35+
c.uncover();
36+
}
37+
}
38+
39+
private ColumnNode selectColumnNodeHeuristic() {
40+
int min = Integer.MAX_VALUE;
41+
ColumnNode ret = null;
42+
for (ColumnNode c = (ColumnNode) header.R; c != header; c = (ColumnNode) c.R) {
43+
if (c.size < min) {
44+
min = c.size;
45+
ret = c;
46+
}
47+
}
48+
return ret;
49+
}
50+
51+
private ColumnNode makeDLXBoard(boolean[][] grid) {
52+
final int COLS = grid[0].length;
53+
54+
ColumnNode headerNode = new ColumnNode("header");
55+
List<ColumnNode> columnNodes = new ArrayList<>();
56+
57+
for (int i = 0; i < COLS; i++) {
58+
ColumnNode n = new ColumnNode(Integer.toString(i));
59+
columnNodes.add(n);
60+
headerNode = (ColumnNode) headerNode.hookRight(n);
61+
}
62+
headerNode = headerNode.R.C;
63+
64+
for (boolean[] aGrid : grid) {
65+
DancingNode prev = null;
66+
for (int j = 0; j < COLS; j++) {
67+
if (aGrid[j]) {
68+
ColumnNode col = columnNodes.get(j);
69+
DancingNode newNode = new DancingNode(col);
70+
if (prev == null)
71+
prev = newNode;
72+
col.U.hookDown(newNode);
73+
prev = prev.hookRight(newNode);
74+
col.size++;
75+
}
76+
}
77+
}
78+
79+
headerNode.size = COLS;
80+
81+
return headerNode;
82+
}
83+
84+
DancingLinks(boolean[][] cover) {
85+
header = makeDLXBoard(cover);
86+
}
87+
88+
public void runSolver() {
89+
answer = new LinkedList<>();
90+
search(0);
91+
}
92+
93+
private void handleSolution(List<DancingNode> answer) {
94+
int[][] result = parseBoard(answer);
95+
printSolution(result);
96+
}
97+
98+
private int size = 9;
99+
100+
private int[][] parseBoard(List<DancingNode> answer) {
101+
int[][] result = new int[size][size];
102+
for (DancingNode n : answer) {
103+
DancingNode rcNode = n;
104+
int min = Integer.parseInt(rcNode.C.name);
105+
for (DancingNode tmp = n.R; tmp != n; tmp = tmp.R) {
106+
int val = Integer.parseInt(tmp.C.name);
107+
if (val < min) {
108+
min = val;
109+
rcNode = tmp;
110+
}
111+
}
112+
int ans1 = Integer.parseInt(rcNode.C.name);
113+
int ans2 = Integer.parseInt(rcNode.R.C.name);
114+
int r = ans1 / size;
115+
int c = ans1 % size;
116+
int num = (ans2 % size) + 1;
117+
result[r][c] = num;
118+
}
119+
return result;
120+
}
121+
122+
private static void printSolution(int[][] result) {
123+
int N = result.length;
124+
for (int[] aResult : result) {
125+
StringBuilder ret = new StringBuilder();
126+
for (int j = 0; j < N; j++) {
127+
ret.append(aResult[j]).append(" ");
128+
}
129+
System.out.println(ret);
130+
}
131+
System.out.println();
132+
}
133+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baeldung.algorithms.sudoku;
2+
3+
import java.util.Arrays;
4+
5+
public class DancingLinksAlgorithm {
6+
private static final int BOARD_SIZE = 9;
7+
private static final int SUBSECTION_SIZE = 3;
8+
private static final int NO_VALUE = 0;
9+
private static final int CONSTRAINTS = 4;
10+
private static final int MIN_VALUE = 1;
11+
private static final int MAX_VALUE = 9;
12+
private static final int COVER_START_INDEX = 1;
13+
14+
private static int[][] board = {
15+
{8, 0, 0, 0, 0, 0, 0, 0, 0},
16+
{0, 0, 3, 6, 0, 0, 0, 0, 0},
17+
{0, 7, 0, 0, 9, 0, 2, 0, 0},
18+
{0, 5, 0, 0, 0, 7, 0, 0, 0},
19+
{0, 0, 0, 0, 4, 5, 7, 0, 0},
20+
{0, 0, 0, 1, 0, 0, 0, 3, 0},
21+
{0, 0, 1, 0, 0, 0, 0, 6, 8},
22+
{0, 0, 8, 5, 0, 0, 0, 1, 0},
23+
{0, 9, 0, 0, 0, 0, 4, 0, 0}
24+
};
25+
26+
public static void main(String[] args) {
27+
DancingLinksAlgorithm solver = new DancingLinksAlgorithm();
28+
solver.solve(board);
29+
}
30+
31+
private void solve(int[][] board) {
32+
boolean[][] cover = initializeExactCoverBoard(board);
33+
DancingLinks dlx = new DancingLinks(cover);
34+
dlx.runSolver();
35+
}
36+
37+
private int getIndex(int row, int col, int num) {
38+
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1);
39+
}
40+
41+
private boolean[][] createExactCoverBoard() {
42+
boolean[][] R = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
43+
44+
int hBase = 0;
45+
46+
// Cell constraint.
47+
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
48+
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
49+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
50+
int index = getIndex(r, c, n);
51+
R[index][hBase] = true;
52+
}
53+
}
54+
}
55+
56+
// Row constrain.
57+
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
58+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
59+
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
60+
int index = getIndex(r, c1, n);
61+
R[index][hBase] = true;
62+
}
63+
}
64+
}
65+
66+
// Column constraint.
67+
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
68+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
69+
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
70+
int index = getIndex(r1, c, n);
71+
R[index][hBase] = true;
72+
}
73+
}
74+
}
75+
76+
// Subsection constraint
77+
for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
78+
for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
79+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
80+
for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
81+
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
82+
int index = getIndex(br + rDelta, bc + cDelta, n);
83+
R[index][hBase] = true;
84+
}
85+
}
86+
}
87+
}
88+
}
89+
return R;
90+
}
91+
92+
private boolean[][] initializeExactCoverBoard(int[][] board) {
93+
boolean[][] R = createExactCoverBoard();
94+
for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) {
95+
for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) {
96+
int n = board[i - 1][j - 1];
97+
if (n != NO_VALUE) {
98+
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
99+
if (num != n) {
100+
Arrays.fill(R[getIndex(i, j, num)], false);
101+
}
102+
}
103+
}
104+
}
105+
}
106+
return R;
107+
}
108+
}

0 commit comments

Comments
 (0)