-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku_Solver.java
143 lines (116 loc) · 5.2 KB
/
Sudoku_Solver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sudoku;
public class Sudoku_Solver {
private static final int Grid_Size =9;
public static void main(String[]args){
// 9 * 9 grid we have to validate and apply algorithm so that
// computer could solve the board
// we need to check individual row validation
// then column validation
// then we could check certain box validation and combine them .
int [][]board =
{
{7,0,2, 0,5,0, 6,0,0},
{0,0,0, 0,0,3, 0,0,0},
{1,0,0, 0,0,9, 5,0,0},
{8,0,0, 0,0,0, 0,9,0},
{0,4,3, 0,0,0, 7,5,0},
{0,9,0, 0,0,0, 0,0,8},
{0,0,9, 7,0,0, 0,0,5},
{0,0,0, 2,0,0, 0,0,0},
{0,0,7, 0,4,0, 2,0,3}
};
System.out.println("Original Board");
printBoard(board);
if (solveBoard(board)){ // if board is valid it would do quick
System.out.println("Solved successfully!");
} // some initial board could cause issue sometimes like impossible to solve
else {
System.out.println("Unsolvable Board :(");
}
printBoard(board);
}
private static void printBoard(int[][] board) {
for ( int row =0 ; row < Grid_Size ; row ++){
if ( row % 3 ==0 & row !=0 ){
System.out.println("---------------");
}
for (int column =0 ; column <Grid_Size ; column ++){
if (column % 3 ==0 & column!=0){
System.out.print(" | ");
}
System.out.print(board[row][column]);
}
System.out.println();
}
}
private static boolean isNumberInRow(int [][]board,int number , int row){
for (int i=0 ; i <Grid_Size; i++){
if (board[row][i] == number){
return true;
}
}
return false;
}
private static boolean isNumberInColumn(int[][] board,int number , int column){
for (int i=0; i < Grid_Size ;i++){
if (board[i][column] == number){
return true;
}
}
return false;
}
// so we need to get the row we could use the mod and get reminder
// like row (that's passed ) -> % 3
// more real case could be row - row % 3 (e.g) row 1
// 1- 1 %3
// =0
// we got at row 0
// that's what we need coordinate of top left corner inside the box
private static boolean isNumberInBox (int [][]board , int number , int row , int column){
int localBoxRow = row - row % 3;
int localBoxColumn = column - column % 3;
for (int i= localBoxRow;i<localBoxRow+3;i++){
for (int j =localBoxColumn ; j<localBoxColumn +3 ;j++){
if(board[i][j]==number){
return true;
}
}
}
return false;
}
private static boolean isValidPlacement(int [][]board,int number , int row , int column){
return !isNumberInRow(board,number,row)&&
!isNumberInColumn(board,number,column)&&
!isNumberInBox(board,number,row,column) ;
}
// this method has ability to backtrack and check previous number it placed but could not solve
// full board later on !
// it can set number back to 0 if failed to solve board by recursion
private static boolean solveBoard(int[][]board){
for (int row =0 ; row <Grid_Size ;row++){ // all rows
for (int column =0 ; column <Grid_Size ;column++){ // all columns in row
if(board[row][column] ==0){ // taking 0 as a blank to tell computer
for (int numberToTry =1 ; numberToTry<=Grid_Size ; numberToTry ++){ // check number 1 to 9
// already have isValidPlacement method
if(isValidPlacement(board,numberToTry,row,column)){ // if it's a valid placement we place
// the number there
board[row][column]=numberToTry;
// now recursion of this algorithm
// method would start again and check for next blank spots means 0 to place to valid number there
if(solveBoard(board)){
return true;
}
else {
board[row][column]=0; // set back to zero if it could not solve
// even if a number is valid, but we could get a case board could not be solved
// if recursion does not work we set it back to 0;
}
}
}
return false; // return if it could not place any valid number after checking all numbers
}
}
}
return true; // solved finally after checking and doing all steps!
}
}