diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java index ff426cbe68..4b37558aab 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -40,15 +40,15 @@ public class BacktrackingAlgorithm { } private boolean solve(int[][] board) { - for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) { - for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) { - if (board[r][c] == NO_VALUE) { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + if (board[row][column] == NO_VALUE) { for (int k = MIN_VALUE; k <= MAX_VALUE; k++) { - board[r][c] = k; - if (isValid(board, r, c) && solve(board)) { + board[row][column] = k; + if (isValid(board, row, column) && solve(board)) { return true; } - board[r][c] = NO_VALUE; + board[row][column] = NO_VALUE; } return false; } @@ -57,44 +57,44 @@ public class BacktrackingAlgorithm { return true; } - private boolean isValid(int[][] board, int r, int c) { - return rowConstraint(board, r) && - columnConstraint(board, c) && - subsectionConstraint(board, r, c); + private boolean isValid(int[][] board, int row, int column) { + return rowConstraint(board, row) && + columnConstraint(board, column) && + subsectionConstraint(board, row, column); } - private boolean subsectionConstraint(int[][] board, int r, int c) { + private boolean subsectionConstraint(int[][] board, int row, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; - int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE; - int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE; - for (int i = subsectionRowStart; i < subsectionRowEnd; i++) { - for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) { - if (!checkConstraint(board, i, constraint, j)) return false; + for (int r = subsectionRowStart; r < subsectionRowEnd; r++) { + for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) { + if (!checkConstraint(board, r, constraint, c)) return false; } } return true; } - private boolean columnConstraint(int[][] board, int c) { + private boolean columnConstraint(int[][] board, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, i, constraint, c)); + .allMatch(row -> checkConstraint(board, row, constraint, column)); } - private boolean rowConstraint(int[][] board, int r) { + private boolean rowConstraint(int[][] board, int row) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, r, constraint, i)); + .allMatch(column -> checkConstraint(board, row, constraint, column)); } - private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) { - if (board[r][c] != NO_VALUE) { - if (!constraint[board[r][c] - 1]) { - constraint[board[r][c] - 1] = true; + private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) { + if (board[row][column] != NO_VALUE) { + if (!constraint[board[row][column] - 1]) { + constraint[board[row][column] - 1] = true; } else { return false; } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java index 76b686afa6..df02ff3d11 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -34,8 +34,8 @@ public class DancingLinksAlgorithm { dlx.runSolver(); } - private int getIndex(int row, int col, int num) { - return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1); + private int getIndex(int row, int column, int num) { + return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1); } private boolean[][] createExactCoverBoard() { @@ -51,12 +51,12 @@ public class DancingLinksAlgorithm { } private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) { - for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { - for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) { - for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) { - int index = getIndex(br + rDelta, bc + cDelta, n); + for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) { + for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) { + int index = getIndex(row + rowDelta, column + columnDelta, n); coverBoard[index][hBase] = true; } } @@ -67,10 +67,10 @@ public class DancingLinksAlgorithm { } private int checkColumnConstraint(boolean[][] coverBoard, int hBase) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { 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); + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -79,10 +79,10 @@ public class DancingLinksAlgorithm { } private int checkRowConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { 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); + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -91,10 +91,10 @@ public class DancingLinksAlgorithm { } private int checkCellConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { - int index = getIndex(r, c, n); + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -104,13 +104,13 @@ public class DancingLinksAlgorithm { private boolean[][] initializeExactCoverBoard(int[][] board) { boolean[][] coverBoard = createExactCoverBoard(); - for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) { - for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) { - int n = board[i - 1][j - 1]; + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int n = board[row - 1][column - 1]; if (n != NO_VALUE) { for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { if (num != n) { - Arrays.fill(coverBoard[getIndex(i, j, num)], false); + Arrays.fill(coverBoard[getIndex(row, column, num)], false); } } } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java index b494eba9ef..2422ff0dff 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -4,21 +4,21 @@ class DancingNode { DancingNode L, R, U, D; ColumnNode C; - DancingNode hookDown(DancingNode n1) { - assert (this.C == n1.C); - n1.D = this.D; - n1.D.U = n1; - n1.U = this; - this.D = n1; - return n1; + DancingNode hookDown(DancingNode node) { + assert (this.C == node.C); + node.D = this.D; + node.D.U = node; + node.U = this; + this.D = node; + return node; } - DancingNode hookRight(DancingNode n1) { - n1.R = this.R; - n1.R.L = n1; - n1.L = this; - this.R = n1; - return n1; + DancingNode hookRight(DancingNode node) { + node.R = this.R; + node.R.L = node; + node.L = this; + this.R = node; + return node; } void unlinkLR() {