-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
152 lines (151 loc) · 4.5 KB
/
Copy pathSolver.java
File metadata and controls
152 lines (151 loc) · 4.5 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
import java.util.Arrays;
public class Solver {
private Cell[][] grid;
private Row[] rows;
private Column[] columns;
private Block[] blocks;
Solver(Cell[] clues) throws Exception{
initialise(clues);
}
private void initialise(Cell[] clues) throws Exception{
grid = new Cell[9][9];
rows = new Row[9];
for(int i = 0;i<9;i++){
rows[i] = new Row();
}
columns = new Column[9];
for(int i = 0;i<9;i++){
columns[i] = new Column();
}
blocks = new Block[9];
for(int i=0;i<9;i++){
blocks[i]=new Block();
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
Cell toAdd = new Cell(i, j);
for(Cell clue:clues){
Integer[] position = new Integer[]{i, j};
if(Arrays.equals(clue.getPosition(), position)){
toAdd = clue;
}
}
grid[i][j] = toAdd;
if(toAdd.getValue()!=null) {
columns[i].addNumber(toAdd);
rows[j].addNumber(toAdd);
blocks[findBlock(i, j)].addNumber(toAdd);
}
}
}
}
private int findBlock(int i, int j){
if(i<=2){
if(j<=2){
return 0;
}else if(j<=5){
return 3;
}else{
return 6;
}
}else if(i<=5){
if(j<=2){
return 1;
}else if(j<=5){
return 4;
}else{
return 7;
}
}else{
if(j<=2){
return 2;
}else if(j<=5){
return 5;
}else{
return 8;
}
}
}
private boolean isPossible(int i, int j, int value){
if(columns[i].hasNumber(value)){
return false;
}else if(rows[j].hasNumber(value)){
return false;
}else return !blocks[findBlock(i, j)].hasNumber(value);
}
public void solveGrid() {
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
Cell cell = grid[i][j];
if(cell.isFixed()){
continue;
}
int initVal;
if(cell.getValue()==null){
initVal=0;
}else{
initVal=cell.getValue();
}
for (int k=initVal+1;k<=10;k++){
if(k<10){
if(isPossible(i, j, k)){
updateCell(i, j, k);
break;
}
}else{
updateCell(i,j,0);
j-=2;
if(j<0){
i-=1;
j=j+9;
}
Cell nextCell;
if(j==8){
nextCell = grid[i+1][0];
}else{
nextCell = grid[i][j+1];
}
while (nextCell.isFixed()){
j-=1;
if(j<0){
i-=1;
j=j+9;
}
if(j==8){
nextCell = grid[i+1][0];
}else{
nextCell = grid[i][j+1];
}
}
}
}
}
}
}
private void updateCell(int i, int j, int value){
Cell cell = grid[i][j];
if(value>0) {
updateCell(i, j, 0);
cell.setValue(value);
try {
columns[i].addNumber(cell);
rows[j].addNumber(cell);
blocks[findBlock(i, j)].addNumber(cell);
} catch (Exception e) {
e.printStackTrace();
}
}
else{
if(cell.getValue()!=null) {
value = cell.getValue();
columns[i].removeNumber(value);
rows[j].removeNumber(value);
blocks[findBlock(i, j)].removeNumber(value);
cell.setValue(null);
}
}
}
public Cell[][] getGrid() {
return grid;
}
}