-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
229 lines (199 loc) · 6.95 KB
/
Sudoku.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Sudoku.java
*
* Implementation of a class that represents a Sudoku puzzle and solves
* it using recursive backtracking.
*
* Name: Selena Zheng
*
*/
import java.io.*;
import java.util.*;
public class Sudoku {
// The current contents of the cells of the puzzle.
private int[][] grid;
/*
* Indicates whether the value in a given cell is fixed
* (i.e., part of the initial configuration).
* valIsFixed[r][c] is true if the value in the cell
* at row r, column c is fixed, and false otherwise.
*/
private boolean[][] valIsFixed;
/*
* This 3-D array allows us to determine if a given subgrid (i.e.,
* a given 3x3 region of the puzzle) already contains a given
* value. We use 2 indices to identify a given subgrid:
*
* (0,0) (0,1) (0,2)
*
* (1,0) (1,1) (1,2)
*
* (2,0) (2,1) (2,2)
*
* For example, subgridHasVal[0][2][5] will be true if the subgrid
* in the upper right-hand corner already has a 5 in it, and false
* otherwise.
*/
private boolean[][][] subgridHasVal;
private boolean[][] rowExist;
private boolean[][] colExist;
/*
* Constructs a new Puzzle object, which initially
* has all empty cells.
*/
public Sudoku() {
this.grid = new int[9][9];
this.valIsFixed = new boolean[9][9];
/*
* Note that the third dimension of the following array is 10,
* because we need to be able to use the possible values
* (1 through 9) as indices.
*/
this.subgridHasVal = new boolean[3][3][10];
this.rowExist = new boolean[9][10];
this.colExist = new boolean[9][10];
}
/*
* Place the specified value in the cell with the specified
* coordinates, and update the state of the puzzle accordingly.
*/
public void placeVal(int val, int row, int col) {
this.grid[row][col] = val;
this.subgridHasVal[row/3][col/3][val] = true;
this.rowExist[row][val] = true;
this.colExist[col][val] = true;
}
/*
* remove the specified value from the cell with the specified
* coordinates, and update the state of the puzzle accordingly.
*/
public void removeVal(int val, int row, int col) {
this.grid[row][col] = 0;
this.subgridHasVal[row/3][col/3][val] = false;
this.rowExist[row][val] = false;
this.colExist[col][val] = false;
}
/*
* read in the initial configuration of the puzzle from the specified
* Scanner, and use that config to initialize the state of the puzzle.
* The configuration should consist of one line for each row, with the
* values in the row specified as integers separated by spaces.
* A value of 0 should be used to indicate an empty cell.
*
* You should not change this method.
*/
public void readConfig(Scanner input) {
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
int val = input.nextInt();
this.placeVal(val, r, c);
if (val != 0) {
this.valIsFixed[r][c] = true;
}
}
input.nextLine();
}
}
/*
* Displays the current state of the puzzle.
* You should not change this method.
*/
public void printGrid() {
for (int r = 0; r < 9; r++) {
this.printRowSeparator();
for (int c = 0; c < 9; c++) {
System.out.print("|");
if (this.grid[r][c] == 0) {
System.out.print(" ");
} else {
System.out.print(" " + this.grid[r][c] + " ");
}
}
System.out.println("|");
}
this.printRowSeparator();
}
// A private helper method used by display()
// to print a line separating two rows of the puzzle.
private static void printRowSeparator() {
for (int i = 0; i < 9; i++) {
System.out.print("----");
}
System.out.println("-");
}
private boolean isSafe(int row, int col, int n){
return(!this.valIsFixed[row][col]&&!this.subgridHasVal[row/3][col/3][n]
&&!this.rowExist[row][n]&&!this.colExist[col][n]);
}
/*
* This is the key recursive-backtracking method. Returns true if
* a solution has already been found, and false otherwise.
*
* Each invocation of the method is responsible for finding the
* value of a single cell of the puzzle. The parameter n
* is the number of the cell that a given invocation of the method
* is responsible for. We recommend that you consider the cells
* one row at a time, from top to bottom and left to right,
* which means that they would be numbered as follows:
*
* 0 1 2 3 4 5 6 7 8
* 9 10 11 12 13 14 15 16 17
* 18 ...
*/
private boolean solveRB(int n) {
/*
* The following return statement allows the initial code to
* compile. Replace it with your full implementation of the
* recursive-backtracking method.
*/
if ( n > 80){
return true;
}
if (this.valIsFixed[n/9][n%9] == true){
return this.solveRB(n+1);
}
for (int i = 0; i < 10; i++){
if (this.isSafe(n/9, n%9, i)){
this.placeVal(i, n/9, n%9);
if(this.solveRB(n+1)){
return true;
}
this.removeVal(i, n/9, n%9);
}
}
return false;
}
/*
* public "wrapper" method for solveRB().
* Makes the initial call to solveRB, and returns whatever it returns.
*/
public boolean solve() {
boolean foundSol = this.solveRB(0);
return foundSol;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Sudoku puzzle = new Sudoku();
System.out.print("Enter the name of the puzzle file: ");
String filename = scan.nextLine();
try {
Scanner input = new Scanner(new File(filename));
puzzle.readConfig(input);
} catch (IOException e) {
System.out.println("error accessing file " + filename);
System.out.println(e);
System.exit(1);
}
System.out.println();
System.out.println("Here is the initial puzzle: ");
puzzle.printGrid();
System.out.println();
if (puzzle.solve()) {
System.out.println("Here is the solution: ");
} else {
System.out.println("No solution could be found.");
System.out.println("Here is the current state of the puzzle:");
}
puzzle.printGrid();
}
}