-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionA.java
More file actions
168 lines (134 loc) · 4.01 KB
/
Copy pathVersionA.java
File metadata and controls
168 lines (134 loc) · 4.01 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package CS486AI.A1Q4;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.FileReader;
import java.io.BufferedReader;
public class VersionA {
public static int counter;
private static boolean isValid(int row, int column, int[][]sudoGrid, int target){
counter++;
// Check if target has already appeared, if false, backtrack
for (int checker =0; checker <9; checker++){
if (sudoGrid[row][checker] == target)
return false;
}
// Check if target has already appeared, if false, backtrack
for (int checker =0; checker <9; checker++){
if (sudoGrid[checker][column] == target)
return false;
}
/* check target is already in sub-grid of the SUDOKU board
* rowSection & columnSection is to define which section of the board
* i.e: For Row
* row 0 -2 belongs to section 0
* row 3-5 belongs to section 3
* row 6-8 belongs to section 6
* Note: Logic applies to column too.
*/
int rowSection = (row/3)*3;
int columnSection = (column/3)*3;
for (int x = 0; x < 3 ; x++){
for (int y = 0; y < 3; y++){
if (sudoGrid[x + rowSection][y + columnSection] == target){
return false;
}
}
}
//Pass all tests for target, return true to proceed to next cell of board
return true;
}
private static boolean sudoSolverV1(int row, int column, int[][] sudoGrid) {
//TODO Backtracking with DFS
// Check if SUDOKU is solved - recursion end case
if (row == 9){
row = 0;
if (++column == 9){
column = 0;
//SUDOKU SOLVED
return true;
}
}
// Solver can't solve Problem, return
else if (counter > 10000)
return false;
//Bypass if row and column value !=0
if (sudoGrid[row][column] != 0)
return (sudoSolverV1(row +1, column, sudoGrid));
for (int target = 1; target <= 9; ++target){
//counter++;
if (isValid(row, column, sudoGrid, target)){
// assigned target to cell of row and column
sudoGrid[row][column] = target;
if (sudoSolverV1(row +1, column, sudoGrid))
return true;
}
}
// failed assignment, backtrack here
sudoGrid[row][column] = 0;
return false;
}
@SuppressWarnings("unused")
private static void printGrid(int [][] sudoGrid) {
// TODO Auto-generated method stub
System.out.println("\nSudoku board");
for (int i =0;i<9;i++){
String line = "";
for (int j=0; j<9; j++){
line += sudoGrid[i][j] + " ";
}
System.out.println(line + "\t");
}
}
public static int [][] loadGrid(int file, int problem){
int result[][] = new int[9][9];
// TODO write codes to load sudo board
try{
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(new FileReader("CS486AI/A1Q4/problems/" + file + "/" + problem + ".sd"));
String output;
for (int i = 0; i < 9; i++){
output = reader.readLine();
StringTokenizer defaultTokenizer = new StringTokenizer(output);
for (int j = 0; j < 9; j++)
result[i][j] = Integer.parseInt(defaultTokenizer.nextToken());
}
}catch (IOException e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int avg = 0;
// USE this to get individual result
counter = 0;
int [][] sudoGrid = loadGrid(1,3);
boolean assignment = sudoSolverV1(0,0,sudoGrid);
System.out.println("Solution for Problem 1, 3.sd");
if (!assignment){
System.out.println("SUDOKU cannot be solved.");
}
else{
System.out.println("SUDOKU Solved. " + counter + " steps");
printGrid(sudoGrid);
System.out.println();
}
System.out.println("Avg varible assignment result per problem folder");
// USE THIS FOR STATS RESULT FOR 71x10 files avg
for (int i = 1; i <= 71; i++){
counter = 0;
for (int j =1; j <=10; j++){
sudoGrid = loadGrid(i,j);
assignment = sudoSolverV1(0,0,sudoGrid);
if (!assignment){
//System.out.println("SUDOKU cannot be solved.");
}
else{
avg++;
//printGrid(sudoGrid);
}
}
System.out.println("File" + i + " : " + counter/avg);
}
}
}