Updated code - mostly with CONSTANTS. Extracted methods.

This commit is contained in:
iaforek 2018-01-28 18:14:29 +00:00
parent d001b23af6
commit 914abdb9a4
2 changed files with 114 additions and 91 deletions

View File

@ -1,18 +1,28 @@
package com.baeldung.algorithms.sudoku; package com.baeldung.algorithms.sudoku;
import java.util.stream.IntStream;
public class BacktrackingAlgorithm { public class BacktrackingAlgorithm {
static char[][] board = { private static int BOARD_SIZE = 9;
{ '8', '.', '.', '.', '.', '.', '.', '.', '.' }, private static int SUBSECTION_SIZE = 3;
{ '.', '.', '3', '6', '.', '.', '.', '.', '.' }, private static int BOARD_INDEX_START = 0;
{ '.', '7', '.', '.', '9', '.', '2', '.', '.' },
{ '.', '5', '.', '.', '.', '7', '.', '.', '.' }, private static int NO_VALUE = 0;
{ '.', '.', '.', '.', '4', '5', '7', '.', '.' }, private static int MIN_VALUE = 1;
{ '.', '.', '.', '1', '.', '.', '.', '3', '.' }, private static int MAX_VALUE = 9;
{ '.', '.', '1', '.', '.', '.', '.', '6', '8' },
{ '.', '.', '8', '5', '.', '.', '.', '1', '.' }, public static int[][] board = {
{ '.', '9', '.', '.', '.', '.', '4', '.', '.' } { 8, 0, 0, 0, 0, 0, 0, 0, 0 },
}; { 0, 0, 3, 6, 0, 0, 0, 0, 0 },
{ 0, 7, 0, 0, 9, 0, 2, 0, 0 },
{ 0, 5, 0, 0, 0, 7, 0, 0, 0 },
{ 0, 0, 0, 0, 4, 5, 7, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 3, 0 },
{ 0, 0, 1, 0, 0, 0, 0, 6, 8 },
{ 0, 0, 8, 5, 0, 0, 0, 1, 0 },
{ 0, 9, 0, 0, 0, 0, 4, 0, 0 }
};
public static void main(String[] args) { public static void main(String[] args) {
BacktrackingAlgorithm solver = new BacktrackingAlgorithm(); BacktrackingAlgorithm solver = new BacktrackingAlgorithm();
@ -21,24 +31,24 @@ public class BacktrackingAlgorithm {
} }
public void printBoard() { public void printBoard() {
for (int i = 0; i < 9; i++) { IntStream.range(BOARD_INDEX_START, BOARD_SIZE).forEach(row -> {
for (int j = 0; j < 9; j++) { IntStream.range(BOARD_INDEX_START, BOARD_SIZE).forEach(column -> {
System.out.print(board[i][j] + " "); System.out.print(board[row][column] + " ");
} });
System.out.println(); System.out.println();
} });
} }
public boolean solve(char[][] board) { public boolean solve(int[][] board) {
for (int r = 0; r < board.length; r++) { for (int r = BOARD_INDEX_START; r < BOARD_SIZE; r++) {
for (int c = 0; c < board[0].length; c++) { for (int c = BOARD_INDEX_START; c < BOARD_SIZE; c++) {
if (board[r][c] == '.') { if (board[r][c] == NO_VALUE) {
for (int k = 1; k <= 9; k++) { for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
board[r][c] = (char) ('0' + k); board[r][c] = k;
if (isValid(board, r, c) && solve(board)) { if (isValid(board, r, c) && solve(board)) {
return true; return true;
} else { } else {
board[r][c] = '.'; board[r][c] = NO_VALUE;
} }
} }
return false; return false;
@ -48,39 +58,44 @@ public class BacktrackingAlgorithm {
return true; return true;
} }
public boolean isValid(char[][] board, int r, int c) { public boolean isValid(int[][] board, int r, int c) {
boolean[] row = new boolean[9]; return (rowConstraint(board, r) &&
for (int i = 0; i < 9; i++) { columnConstraint(board, c) &&
if (board[r][i] >= '1' && board[r][i] <= '9') { subsectionConstraint(board, r, c));
if (row[board[r][i] - '1'] == false) { }
row[board[r][i] - '1'] = true;
} else { private boolean subsectionConstraint(int[][] board, int r, int c) {
return false; boolean[] constraint = new boolean[BOARD_SIZE];
} for (int i = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; i < (r / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; i++) {
for (int j = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; j < (c / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; j++) {
if (!checkConstraint(board, i, constraint, j)) return false;
} }
} }
return true;
}
boolean[] col = new boolean[9]; private boolean columnConstraint(int[][] board, int c) {
for (int i = 0; i < 9; i++) { boolean[] constraint = new boolean[BOARD_SIZE];
if (board[i][c] >= '1' && board[i][c] <= '9') { for (int i = BOARD_INDEX_START; i < BOARD_SIZE; i++) {
if (col[board[i][c] - '1'] == false) { if (!checkConstraint(board, i, constraint, c)) return false;
col[board[i][c] - '1'] = true;
} else {
return false;
}
}
} }
return true;
}
boolean[] grid = new boolean[9]; private boolean rowConstraint(int[][] board, int r) {
for (int i = (r / 3) * 3; i < (r / 3) * 3 + 3; i++) { boolean[] constraint = new boolean[BOARD_SIZE];
for (int j = (c / 3) * 3; j < (c / 3) * 3 + 3; j++) { for (int i = BOARD_INDEX_START; i < BOARD_SIZE; i++) {
if (board[i][j] >= '1' && board[i][j] <= '9') { if (!checkConstraint(board, r, constraint, i)) return false;
if (grid[board[i][j] - '1'] == false) { }
grid[board[i][j] - '1'] = true; return true;
} else { }
return false;
} private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) {
} if (board[r][c] >= MIN_VALUE && board[r][c] <= MAX_VALUE) {
if (constraint[board[r][c] - 1] == false) {
constraint[board[r][c] - 1] = true;
} else {
return false;
} }
} }
return true; return true;

View File

@ -3,28 +3,32 @@ package com.baeldung.algorithms.sudoku;
import java.util.*; import java.util.*;
public class DancingLinksAlgorithm { public class DancingLinksAlgorithm {
private static int S = 9; private static int BOARD_SIZE = 9;
private static int SIDE = 3; private static int SUBSECTION_SIZE = 3;
private static int NO_VALUE = 0;
private static int CONSTRAINTS = 4;
private static int MIN_VALUE = 1;
private static int MAX_VALUE = 9;
private static int COVER_START_INDEX = 1;
static char[][] board = { public static int[][] board = {
{ '8', '.', '.', '.', '.', '.', '.', '.', '.' }, { 8, 0, 0, 0, 0, 0, 0, 0, 0 },
{ '.', '.', '3', '6', '.', '.', '.', '.', '.' }, { 0, 0, 3, 6, 0, 0, 0, 0, 0 },
{ '.', '7', '.', '.', '9', '.', '2', '.', '.' }, { 0, 7, 0, 0, 9, 0, 2, 0, 0 },
{ '.', '5', '.', '.', '.', '7', '.', '.', '.' }, { 0, 5, 0, 0, 0, 7, 0, 0, 0 },
{ '.', '.', '.', '.', '4', '5', '7', '.', '.' }, { 0, 0, 0, 0, 4, 5, 7, 0, 0 },
{ '.', '.', '.', '1', '.', '.', '.', '3', '.' }, { 0, 0, 0, 1, 0, 0, 0, 3, 0 },
{ '.', '.', '1', '.', '.', '.', '.', '6', '8' }, { 0, 0, 1, 0, 0, 0, 0, 6, 8 },
{ '.', '.', '8', '5', '.', '.', '.', '1', '.' }, { 0, 0, 8, 5, 0, 0, 0, 1, 0 },
{ '.', '9', '.', '.', '.', '.', '4', '.', '.' } { 0, 9, 0, 0, 0, 0, 4, 0, 0 }
}; };
public static void main(String[] args) { public static void main(String[] args) {
DancingLinksAlgorithm solver = new DancingLinksAlgorithm(); DancingLinksAlgorithm solver = new DancingLinksAlgorithm();
solver.solve(board); solver.solve(board);
} }
public boolean solve(char[][] board) { public boolean solve(int[][] board) {
boolean[][] cover = initializeExactCoverBoard(board); boolean[][] cover = initializeExactCoverBoard(board);
DancingLinks dlx = new DancingLinks(cover); DancingLinks dlx = new DancingLinks(cover);
dlx.runSolver(); dlx.runSolver();
@ -33,46 +37,50 @@ public class DancingLinksAlgorithm {
} }
private int getIndex(int row, int col, int num) { private int getIndex(int row, int col, int num) {
return (row - 1) * S * S + (col - 1) * S + (num - 1); return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1);
} }
private boolean[][] createExactCoverBoard() { private boolean[][] createExactCoverBoard() {
boolean[][] R = new boolean[9 * 9 * 9][9 * 9 * 4]; boolean[][] R = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
int hBase = 0; int hBase = 0;
for (int r = 1; r <= S; r++) { // Cell constraint.
for (int c = 1; c <= S; c++, hBase++) { for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
for (int n = 1; n <= S; n++) { for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
int index = getIndex(r, c, n); int index = getIndex(r, c, n);
R[index][hBase] = true; R[index][hBase] = true;
} }
} }
} }
for (int r = 1; r <= S; r++) { // Row constrain.
for (int n = 1; n <= S; n++, hBase++) { for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
for (int c1 = 1; c1 <= S; c1++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
int index = getIndex(r, c1, n); int index = getIndex(r, c1, n);
R[index][hBase] = true; R[index][hBase] = true;
} }
} }
} }
for (int c = 1; c <= S; c++) { // Column constraint.
for (int n = 1; n <= S; n++, hBase++) { for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
for (int r1 = 1; r1 <= S; r1++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
int index = getIndex(r1, c, n); int index = getIndex(r1, c, n);
R[index][hBase] = true; R[index][hBase] = true;
} }
} }
} }
for (int br = 1; br <= S; br += SIDE) { // Subsection constraint
for (int bc = 1; bc <= S; bc += SIDE) { for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
for (int n = 1; n <= S; n++, hBase++) { for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
for (int rDelta = 0; rDelta < SIDE; rDelta++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int cDelta = 0; cDelta < SIDE; cDelta++) { for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
int index = getIndex(br + rDelta, bc + cDelta, n); int index = getIndex(br + rDelta, bc + cDelta, n);
R[index][hBase] = true; R[index][hBase] = true;
} }
@ -83,14 +91,14 @@ public class DancingLinksAlgorithm {
return R; return R;
} }
private boolean[][] initializeExactCoverBoard(char[][] sudoku) { private boolean[][] initializeExactCoverBoard(int[][] board) {
boolean[][] R = createExactCoverBoard(); boolean[][] R = createExactCoverBoard();
for (int i = 1; i <= S; i++) { for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) {
for (int j = 1; j <= S; j++) { for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) {
char n = sudoku[i - 1][j - 1]; int n = board[i - 1][j - 1];
if (n != '.') { if (n != NO_VALUE) {
for (int num = 1; num <= S; num++) { for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
if ((char) ('0' + num) != n) { if (num != n) {
Arrays.fill(R[getIndex(i, j, num)], false); Arrays.fill(R[getIndex(i, j, num)], false);
} }
} }