-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuudoku.java
executable file
·230 lines (203 loc) · 5.06 KB
/
Suudoku.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
/**
*
*/
public class Suudoku {
public static void main(String[] ignored){
Suudoku obj = new Suudoku(9);
int [][] prob = {
{0,0,0,0,0,0,2,0,0},
{0,6,5,0,0,3,0,0,4},
{2,0,0,0,0,6,0,0,0},
{1,0,8,0,0,0,9,0,0},
{3,0,7,8,0,1,0,0,0},
{0,0,0,4,0,0,0,5,0},
{0,0,0,2,0,0,0,0,0},
{0,0,0,7,0,0,3,0,0},
{0,0,9,0,8,0,7,0,0},
};
obj.setProblem(prob);
obj.solve();
System.out.println(obj);
}
int size;
int curx, cury;
int problem[][];
int solution[][];
public Suudoku(int size){
this.size=size;
problem = new int[size][];
for(int i = 0 ; i < size ; i++){
problem[i]= new int[size];
}
solution = new int[size][];
for(int i = 0 ; i < size ; i++){
solution[i]= new int[size];
}
curx = -1;
cury = -1;
}
public void setProblem(int[][] problemdata){
for(int i = 0;i<size;i++){
for(int j = 0 ; j<size;j++){
problem[i][j]=problemdata[i][j];
}
}
}
public int[][] solve(){
while(true){
if(needBacktrack()){
backtrack();
}else{
proceed();
}
if((isSolved()) ||(isTerminated())){
System.out.println("solved / terminated.breaks");
break;
}
solveOneCell();
}
// TBD: returns result.
return null;
}
/**
* proposes next value by incrementing the current cell
* if there'S NO next cell, it proposes "size+1" value
* to indicate no proposal available(needs back track)
*/
private void solveOneCell(){
countloop :
do{
solution[curx][cury] +=1;
if(solution[curx][cury] > size) {break;}
for(int i = 0 ; i < size; i++){
if((i != curx) && (solution[curx][cury] == solution[i][cury])){
continue countloop;
}
if((i != curx) && (solution[curx][cury] == problem[i][cury])){
continue countloop;
}
}
for(int i = 0 ; i < size; i++){
if((i != cury )&& (solution[curx][cury] == solution[curx][i])){
continue countloop;
}
if((i != cury )&& (solution[curx][cury] == problem[curx][i])){
continue countloop;
}
}
break;
}while(true);
System.out.println("solved : ("+curx+","+cury+")="+solution[curx][cury]);
}
private boolean isSolved(){
// TBD full implementation
return cury == size;
}
private boolean isTerminated(){
// TBD full implementation
return cury == size;
}
private void proceed(){
System.out.println("proceed from :("+curx+","+cury+")");
do{
proceed_step();
if(isFixedByProblem()){
setFixedValue();
continue;
}
break;
}while(true);
}
private void setFixedValue(){
solution[curx][cury] = problem[curx][cury];
}
private boolean isFixedByProblem(){
if(isOutOfTheMatrix()){return false;}
return 0 != problem[curx][cury];
}
private void proceed_step(){
if(curx <0) {
curx=0;
cury=0;
System.out.println("proceed to :("+curx+","+cury+")");
return;
}
if(curx < size-1){
curx ++;
System.out.println("proceed to :("+curx+","+cury+")");
return;
}
if((curx == size-1)&&(cury < size-1)){
cury ++;
curx = 0;
System.out.println("proceed to :("+curx+","+cury+")");
return;
}
if((curx == size-1)&&(cury == size-1)){
cury ++;
curx ++;
System.out.println("proceed to :("+curx+","+cury+")");
return;
}
System.out.println("proceed error :("+curx+","+cury+")");
// can't be here.do nothing
}
private boolean isOutOfTheMatrix(){
return curx >= size || curx < 0 || cury < 0 || cury >= size;
}
private boolean needBacktrack(){
if(isOutOfTheMatrix()){return false;}
return (solution[curx][cury] > size);
}
/**
* back tracks as it can
*/
private void backtrack(){
System.out.println("backtrack from :("+curx+","+cury+")");
// backtracking occurred at the first point, it selects termination.
do{
backtrack_step();
if(isFixedByProblem()){
continue;
}
break;
}while(true);
}
private void backtrack_step(){
if(isOutOfTheMatrix()){
System.out.println("backtrack error(outof matrix) :("+curx+","+cury+")");
return;
}
solution[curx][cury] = 0;
if((curx == 0) && (cury == 0)){
curx = size;
cury = size;
System.out.println("backtrack to :("+curx+","+cury+")");
return;
}
if(curx <size && curx > 0){
curx --;
System.out.println("backtrack to :("+curx+","+cury+")");
return;
}
if((curx == 0)&&(cury > 0)){
cury --;
curx = size-1;
System.out.println("backtrack to :("+curx+","+cury+")");
return;
}
// can't be here.do nothing
System.out.println("backtrack error :("+curx+","+cury+")");
}
public String toString(){
String result = "";
for(int i = 0; i < size; i++){
result+="[";
for(int j = 0; j < size; j++){
result+=(solution[i][j] + ",");
}
result+="]\n";
}
return result;
}
}