【代码】自动解数独程序

前言

自动解数独程序

源代码

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
package com;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

static class Cell {

// 当前格子存储的数据
public int data;
// 当前格子所在行
public int row;
// 当前格子所在列
public int col;
// 当前格子所在区域
public int part;

public String toString() {
return "{" +
"数据:" + data + "," +
"所在行:" + row + "," +
"所在列" + col + "," +
"所在区域:" + part +
"}";
}

}

public static void main(String[] args) {

System.out.println("说明:");
System.out.println("将数独整张表的所有格子以空格分隔的形式输入,需要填写的格子用0占位");
System.out.println("举例:");
System.out.println("0 0 1 4 0 8 0 3 0 8 0 5 1 0 0 6 0 0 0 7 9 0 0 6 2 8 0 1 0 3 0 2 0 0 7 8 0 0 4 9 0 3 0 0 0 6 2 0 0 4 0 3 0 9 0 1 2 8 0 0 7 6 3 0 0 6 0 0 7 5 0 4 0 4 0 3 0 9 0 0 0");
System.out.println("请输入数独:");

Scanner scanner = new Scanner(System.in);
String allString = scanner.nextLine();

// String allString = "0 0 1 4 0 8 0 3 0 8 0 5 1 0 0 6 0 0 0 7 9 0 0 6 2 8 0 1 0 3 0 2 0 0 7 8 0 0 4 9 0 3 0 0 0 6 2 0 0 4 0 3 0 9 0 1 2 8 0 0 7 6 3 0 0 6 0 0 7 5 0 4 0 4 0 3 0 9 0 0 0";

String[] allArr = allString.split(" ");

Cell[] allObj = new Cell[81];

// System.out.println("收到的数据的个数:" + allObj.length);

// 定义数据和所在行
for (int i = 0; i < 81; i++) {
Cell cell = new Cell();
cell.data = Integer.parseInt(allArr[i]);
allObj[i] = cell;
}

// 定义所在行
for (int i = 0; i < 9; i++) {
allObj[i * 9 + 0].row = i;
allObj[i * 9 + 1].row = i;
allObj[i * 9 + 2].row = i;
allObj[i * 9 + 3].row = i;
allObj[i * 9 + 4].row = i;
allObj[i * 9 + 5].row = i;
allObj[i * 9 + 6].row = i;
allObj[i * 9 + 7].row = i;
allObj[i * 9 + 8].row = i;
}

// 定义所在列
for (int i = 0; i < 9; i++) {
allObj[i + 0 * 9].col = i;
allObj[i + 1 * 9].col = i;
allObj[i + 2 * 9].col = i;
allObj[i + 3 * 9].col = i;
allObj[i + 4 * 9].col = i;
allObj[i + 5 * 9].col = i;
allObj[i + 6 * 9].col = i;
allObj[i + 7 * 9].col = i;
allObj[i + 8 * 9].col = i;
}

// 定义区域
{
allObj[0].part = 1;
allObj[1].part = 1;
allObj[2].part = 1;
allObj[9].part = 1;
allObj[10].part = 1;
allObj[11].part = 1;
allObj[18].part = 1;
allObj[19].part = 1;
allObj[20].part = 1;

allObj[3].part = 2;
allObj[4].part = 2;
allObj[5].part = 2;
allObj[12].part = 2;
allObj[13].part = 2;
allObj[14].part = 2;
allObj[21].part = 2;
allObj[22].part = 2;
allObj[23].part = 2;

allObj[6].part = 3;
allObj[7].part = 3;
allObj[8].part = 3;
allObj[15].part = 3;
allObj[16].part = 3;
allObj[17].part = 3;
allObj[24].part = 3;
allObj[25].part = 3;
allObj[26].part = 3;

allObj[27].part = 4;
allObj[28].part = 4;
allObj[29].part = 4;
allObj[36].part = 4;
allObj[37].part = 4;
allObj[38].part = 4;
allObj[45].part = 4;
allObj[46].part = 4;
allObj[47].part = 4;

allObj[30].part = 5;
allObj[31].part = 5;
allObj[32].part = 5;
allObj[39].part = 5;
allObj[40].part = 5;
allObj[41].part = 5;
allObj[48].part = 5;
allObj[49].part = 5;
allObj[50].part = 5;

allObj[33].part = 6;
allObj[34].part = 6;
allObj[35].part = 6;
allObj[42].part = 6;
allObj[43].part = 6;
allObj[44].part = 6;
allObj[51].part = 6;
allObj[52].part = 6;
allObj[53].part = 6;

allObj[54].part = 7;
allObj[55].part = 7;
allObj[56].part = 7;
allObj[63].part = 7;
allObj[64].part = 7;
allObj[65].part = 7;
allObj[72].part = 7;
allObj[73].part = 7;
allObj[74].part = 7;

allObj[57].part = 8;
allObj[58].part = 8;
allObj[59].part = 8;
allObj[66].part = 8;
allObj[67].part = 8;
allObj[68].part = 8;
allObj[75].part = 8;
allObj[76].part = 8;
allObj[77].part = 8;

allObj[60].part = 9;
allObj[61].part = 9;
allObj[62].part = 9;
allObj[69].part = 9;
allObj[70].part = 9;
allObj[71].part = 9;
allObj[78].part = 9;
allObj[79].part = 9;
allObj[80].part = 9;
}

// 打印所有对象
// for (Cell cell : allObj) {
// System.out.println(cell);
// }

// 打印所有数据
System.out.println("完成前的数独九宫格");
for (int i = 0; i < 81; i++) {
System.out.print(allObj[i].data+" ");
if ((i+1)%9==0) {
System.out.println();
}
}

// 分割线
System.out.println("--------------");



// 开始答题
{
// 设置一个无限循环的开关
boolean key = true;
while (key) {
// 设置一个计数器,用于统计数据不为0的格子
int count = 0;
for (Cell cell : allObj) {
// 如果遍历的当前格子数据不为0,计数器自增
if (cell.data!=0) {
count++;
}
/*
如果此时计数器达到81
将无限循环开关关上
并跳出当前统计的循环
*/
if (count==81) {
key = false;
break;
}
}

/*
查找所有数据为0的格子,进行运算,为它重新赋值
*/
for (Cell cell : allObj) {

if (cell.data==0) {

// 将当前行的所有数据收集起来
int row = cell.row;
List<Integer> rowListForExists = new ArrayList<>();
for (Cell c : allObj) {
if (c.row==row) {
rowListForExists.add(c.data);
}
}

// 将当前列的所有数据收集起来
int col = cell.col;
List<Integer> colListForExists = new ArrayList<>();
for (Cell c : allObj) {
if (c.col==col) {
colListForExists.add(c.data);
}
}

// 将当前区域的所有数据收集起来
int part = cell.part;
List<Integer> partListForExists = new ArrayList<>();
for (Cell c : allObj) {
if (c.part==part) {
partListForExists.add(c.data);
}
}

// 将3组数据用Set集合去重,然后重新收集起来
Set<Integer> rcp = new HashSet<>();
for (Integer rowListForExist : rowListForExists) {
rcp.add(rowListForExist);
}
for (Integer colListForExist : colListForExists) {
rcp.add(colListForExist);
}
for (Integer partListForExist : partListForExists) {
rcp.add(partListForExist);
}
ArrayList<Integer> exist = new ArrayList<>();
for (Integer rcpOne : rcp) {
if (rcpOne!=0) {
exist.add(rcpOne);
}
}
// System.out.println("收集的所有存在的"+exist);

ArrayList<Integer> unExist = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
if (!exist.contains(i)) {
unExist.add(i);
}
}
// System.out.println("收集的所有不存在的"+unExist);

if (unExist.size()==1) {
cell.data = unExist.get(0);
} else {
continue;
}

}
}
}
}

// 答题结束,打印所有数据
System.out.println("完成后的数独九宫格");
for (int i = 0; i < 81; i++) {
System.out.print(allObj[i].data+" ");
if ((i+1)%9==0) {
System.out.println();
}
}



}

}

完成