-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku.java
More file actions
239 lines (205 loc) · 6.93 KB
/
Sudoku.java
File metadata and controls
239 lines (205 loc) · 6.93 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
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
230
231
232
233
234
235
236
237
238
239
/**
* Sudoku.java
*
* Implementation of a class that represents a Sudoku puzzle and solves
* it using recursive backtracking.
import java.io.*; // allows us to read from a file
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;
/*
* 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[9][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;
}
/*
* 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;
}
/*
* 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.
*
*/
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.
*/
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("-");
}
public void testme() {
}
public boolean next(int row, int col) {
if (col < 8) {
return this.solveRB(row, col+1);
} else {
return this.solveRB(row +1, 0);
}
}
//checks to see if number is safe to place here or not
public boolean safety(int num, int row, int col){
//row check
for(int i = 0; i < 9; i++) {
if(this.grid[row][i] == num) {
return false;
}
}
//col check
for(int i = 0; i < 9; i++) {
if(this.grid[i][col] == num) {
return false;
}
}
//box check
if(this.subgridHasVal[row/3][col/3][num]) {
return false;
}
return true;
}
/*
* This is the key recursive-backtracking method.
* Returns true if a solution has already been found, and false otherwise.
*
* There are different ways to use parameters in this method,
* and you will need to decide how many parameters you want to use
* and what they should mean.
*/
private boolean solveRB(int row, int col) {
if (row >= 9) {
return true;
}
if(this.valIsFixed[row][col]) {
if (next(row,col)) {
return true;
}
}
else {
for (int val = 1; val <= 9; val++){
if (safety(val, row, col)) {
this.placeVal(val, row, col);
if (next(row, col)){
return true;
}
}
this.removeVal(this.grid[row][col], row, col);
}
}
/*
* The following return statement allows the initial code to compile.
* Replace it with your full implementation of the recursive-backtracking
* method.
*/
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,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);
}
puzzle.testme();
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();
}
}