33import java .util .stream .IntStream ;
44
55public class BacktrackingAlgorithm {
6-
7- private static int BOARD_SIZE = 9 ;
8- private static int SUBSECTION_SIZE = 3 ;
9- private static int BOARD_START_INDEX = 0 ;
10-
11- private static int NO_VALUE = 0 ;
12- private static int MIN_VALUE = 1 ;
13- private static int MAX_VALUE = 9 ;
146
15- public 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 }
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 }
2525 };
2626
2727 public static void main (String [] args ) {
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030 solver .printBoard ();
3131 }
3232
33- public void printBoard () {
33+ private void printBoard () {
3434 for (int row = BOARD_START_INDEX ; row < BOARD_SIZE ; row ++) {
3535 for (int column = BOARD_START_INDEX ; column < BOARD_SIZE ; column ++) {
3636 System .out .print (board [row ][column ] + " " );
@@ -39,17 +39,16 @@ public void printBoard() {
3939 }
4040 }
4141
42- public boolean solve (int [][] board ) {
42+ private boolean solve (int [][] board ) {
4343 for (int r = BOARD_START_INDEX ; r < BOARD_SIZE ; r ++) {
4444 for (int c = BOARD_START_INDEX ; c < BOARD_SIZE ; c ++) {
4545 if (board [r ][c ] == NO_VALUE ) {
4646 for (int k = MIN_VALUE ; k <= MAX_VALUE ; k ++) {
4747 board [r ][c ] = k ;
4848 if (isValid (board , r , c ) && solve (board )) {
4949 return true ;
50- } else {
51- board [r ][c ] = NO_VALUE ;
5250 }
51+ board [r ][c ] = NO_VALUE ;
5352 }
5453 return false ;
5554 }
@@ -58,16 +57,22 @@ public boolean solve(int[][] board) {
5857 return true ;
5958 }
6059
61- public boolean isValid (int [][] board , int r , int c ) {
62- return ( rowConstraint (board , r ) &&
63- columnConstraint (board , c ) &&
64- subsectionConstraint (board , r , c ) );
60+ private boolean isValid (int [][] board , int r , int c ) {
61+ return rowConstraint (board , r ) &&
62+ columnConstraint (board , c ) &&
63+ subsectionConstraint (board , r , c );
6564 }
6665
6766 private boolean subsectionConstraint (int [][] board , int r , int c ) {
6867 boolean [] constraint = new boolean [BOARD_SIZE ];
69- for (int i = (r / SUBSECTION_SIZE ) * SUBSECTION_SIZE ; i < (r / SUBSECTION_SIZE ) * SUBSECTION_SIZE + SUBSECTION_SIZE ; i ++) {
70- for (int j = (c / SUBSECTION_SIZE ) * SUBSECTION_SIZE ; j < (c / SUBSECTION_SIZE ) * SUBSECTION_SIZE + SUBSECTION_SIZE ; j ++) {
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 ++) {
7176 if (!checkConstraint (board , i , constraint , j )) return false ;
7277 }
7378 }
@@ -76,28 +81,24 @@ private boolean subsectionConstraint(int[][] board, int r, int c) {
7681
7782 private boolean columnConstraint (int [][] board , int c ) {
7883 boolean [] constraint = new boolean [BOARD_SIZE ];
79- for (int i = BOARD_START_INDEX ; i < BOARD_SIZE ; i ++) {
80- if (!checkConstraint (board , i , constraint , c )) return false ;
81- }
82- return true ;
84+ return IntStream .range (BOARD_START_INDEX , BOARD_SIZE )
85+ .allMatch (i -> checkConstraint (board , i , constraint , c ));
8386 }
8487
8588 private boolean rowConstraint (int [][] board , int r ) {
8689 boolean [] constraint = new boolean [BOARD_SIZE ];
87- for (int i = BOARD_START_INDEX ; i < BOARD_SIZE ; i ++) {
88- if (!checkConstraint (board , r , constraint , i )) return false ;
89- }
90- return true ;
90+ return IntStream .range (BOARD_START_INDEX , BOARD_SIZE )
91+ .allMatch (i -> checkConstraint (board , r , constraint , i ));
9192 }
9293
9394 private boolean checkConstraint (int [][] board , int r , boolean [] constraint , int c ) {
94- if (board [r ][c ] >= MIN_VALUE && board [ r ][ c ] <= MAX_VALUE ) {
95- if (constraint [board [r ][c ] - 1 ] == false ) {
95+ if (board [r ][c ] != NO_VALUE ) {
96+ if (! constraint [board [r ][c ] - 1 ]) {
9697 constraint [board [r ][c ] - 1 ] = true ;
9798 } else {
9899 return false ;
99100 }
100101 }
101102 return true ;
102103 }
103- }
104+ }
0 commit comments