-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.java
343 lines (274 loc) · 9.82 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Sudoku {
static ArrayList<String> coordinateX = new ArrayList<String>();
static ArrayList<String> coordinateY = new ArrayList<String>();
public static HashMap<String,Integer> hm = new HashMap<String,Integer>();
public static HashMap<Integer, String> hmresult = new HashMap<Integer, String>();
static BufferedWriter bw;
public Sudoku() {
System.out.println("constractor");
}
public static void main (String[] args) {
System.out.println("masuk class sudoku");
}
public static void carisolusi(int n, ArrayList<String> isiUser, ArrayList<String> hasilMinisat) throws InterruptedException{
try {
int inputUser = isiUser.size();
int jumlahVar = (int) Math.pow(n, 3);
int jumlahKlausa = (4*n*n) + (4* n * n * (combination(n,2))) + inputUser;
String content = "p cnf "+ jumlahVar+" "+jumlahKlausa +"\n" ;
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(content);
encode(n);
hitung(n);
//memasukan constraint yang berasal dari masukan user
if(isiUser.size()!=0){
for(int i =0;i<isiUser.size();i++){
bw.write(hm.get(isiUser.get(i))+" 0"+"\n");
//System.out.println(isiUser.get(i));
}
}
bw.close();
//kode untuk menjalankan minisat
//Process process = Runtime.getRuntime().exec("minisat filename.txt result.txt");
//process.waitFor();
System.out.println("Masih proses minisat");
System.out.println("Done");
/**String[] args = new String[] {"/bin/bash", "-c", "minisat filename.txt result.txt", "", ""};
Process process = new ProcessBuilder(args).start();
process.waitFor();
**/
//membaca output
System.out.println("Masuk br");
BufferedReader br = new BufferedReader(new FileReader("result.txt"));
String line = br.readLine();
if(line.equalsIgnoreCase("SAT")){
System.out.println("Problem Satisfiable");
String barisdua = br.readLine();
System.out.println("Ini baris 2: " +barisdua);
String baristiga = br.readLine();
System.out.println("Ini baris 3: " +baristiga);
String[] angka = barisdua.split(" ");
int hasilSplit=0;
//Meletakkan angka pada koordinat yang benar. Hasil jawaban benar ada pada HashMap hasilMinisat
for(int i = 0;i<angka.length;i++){
hasilSplit= Integer.parseInt(angka[i]);
if(hasilSplit>0){
//System.out.println(""+hasilSplit);
//System.out.println(hasilSplit);
//System.out.println("nilai yang masuk ke minisat"+hmresult.get(hasilSplit));
hasilMinisat.add(""+hmresult.get(hasilSplit));
//System.out.println(""+hmresult.get(hasilSplit));
}
}
System.out.println("KOORDINAT YANG BENAR");
for(int i =0; i< hasilMinisat.size();i++){
String tmp = hasilMinisat.get(i);
//System.out.println(hasilMinisat.get(i));
String[] splitOrdinat = tmp.split(",");
int x = Integer.parseInt(splitOrdinat[0]) -1;
int y = Integer.parseInt(splitOrdinat[1]) -1;
int value = Integer.parseInt(splitOrdinat[2]) ;
}
}else{
System.out.println("Tidak ada solusi!");
SudokuGUI.infoBox("Sorry, your input is unsatisfiable" + n, "Unsatifiable");
hasilMinisat.clear();
isiUser.clear();
}
//System.out.println(line);
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int combination(int n, int k)
{
return (fact(n) / (fact(k) * fact(n - k)));
}
public static int fact(int i)
{
if (i == 1)
{
return 1;
}
return i * fact(i - 1);
}
public static void encode(int n){
int code = 1;
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<=n;k++){
hm.put(""+i+","+j+","+k, code); //hm key : koordinat value : angka
hmresult.put(code, ""+i+","+j+","+k); //hmresult key: angka , value : koordinat
code++;
}
}
}
}
public static void hitung(int n) throws IOException{
int hit = 0;
//mengecek pada setiap field at least diisi satu digit
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<=n;k++){
if(k==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek pada setiap field hanya ada satu digit
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<n;k++){
for (int m = k+1; m <=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+j+","+ m)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+j+","+ m)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap baris at least ada satu digit
for(int j = 1; j<=n;j++){
for(int k = 1; k<=n;k++){
for(int i =1;i<=n;i++){
if(i==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap baris cuma terisi satu digit
for(int j = 1; j<=n;j++){
for(int k = 1; k<=n;k++){
for(int i =1;i<n;i++){
for (int m = (i+1); m<=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(m+","+j+","+ k)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(m+","+j+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap kolom at least ada satu digit
for(int i = 1; i<=n;i++){
for(int k = 1; k<=n;k++){
for(int j =1;j<=n;j++){
if(j==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap kolom hanya ada satu digit
for(int i = 1; i<=n;i++){
for(int k = 1; k<=n;k++){
for(int j =1;j<n;j++){
for (int m = j+1; m<=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+m+","+ k)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+m+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap subgrid at least ada satu digit
int akar = (int) Math.sqrt(n);
int kelipatan=1;
for(int k = 1;k<=n;k++){
for(int m = 0;m<=(akar-1);m++){
for(int p =0;p<=(akar-1);p++){
for(int i=1;i<=akar;i++){
for(int j=1;j<=akar;j++){
if(kelipatan%n==0){
kelipatan=0;
//System.out.println((""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0" );
//System.out.println(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0" );
bw.write(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0"+"\n" );
kelipatan++;
hit +=1;
}else{
//System.out.print(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" " );
bw.write(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" ");;
kelipatan++;
}
}
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap subgrid at most cuma ada 1 digit
for(int k = 1; k<=n;k++){
for(int m=0; m<=(akar-1); m++){
for(int p =0; p<=(akar-1);p++){
for(int i = 1;i<=akar;i++){
for(int j=1;j<=akar;j++){
for(int q = j+1;q<=akar;q++){
//System.out.println("-"+(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+(((akar*m)+i)+","+((akar*p)+q)+","+ k)+" 0");
bw.write("-"+hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+hm.get(((akar*m)+i)+","+((akar*p)+q)+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
for(int k = 1; k<=n;k++)
for(int m=0; m<=(akar-1); m++){
for(int p =0; p<=(akar-1);p++){
for(int i = 1;i<=akar;i++){
for(int j=1;j<=akar;j++){
for(int q = i+1;q<=akar;q++){
for(int r = 1;r<=akar;r++){
//System.out.println("-"+(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+(((akar*m)+q)+","+((akar*p)+r)+","+ k)+" 0");
bw.write("-"+hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+hm.get(((akar*m)+q)+","+((akar*p)+r)+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
}
}
}
}