-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuService.cs
283 lines (269 loc) · 8.85 KB
/
SudokuService.cs
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
using System;
using System.Collections.Generic;
using System.Text;
/* =====================================
* Author: 朱乙
* Blog: http://blog.csdn.net/sq_zhuyi
* Email: sqzhuyi@gmail.com
======================================== */
namespace SudokuResponder
{
public class SudokuService
{
Number[][][][] pArr = null;//存储数独数字
public SudokuService(string numStr)
{
string s = numStr.Replace(",", "");
if (s.Length != 81)
{
throw new Exception("初始数独错误,必须是81个数字");
}
int[] numArr = new int[81];
for (int i = 0; i < s.Length; i++)
{
numArr[i] = Convert.ToInt32(s[i].ToString());
}
pArr = GetArray(numArr);
}
public SudokuService(int[] numArr)
{
if (numArr.Length != 81)
{
throw new Exception("初始数独错误,必须是81个数字");
}
pArr = GetArray(numArr);
}
/// <summary>
/// 获取结果
/// </summary>
/// <returns></returns>
public string GetResult()
{
FillArray();
StringBuilder sb = new StringBuilder();
for (int m = 0; m < 3; m++)
{
for (int i = 0; i < 3; i++)
{
for (int n = 0; n < 3; n++)
{
for (int j = 0; j < 3; j++)
{
sb.Append(pArr[m][n][i][j].Value);
}
}
sb.Append(",");
}
}
return sb.ToString().TrimEnd(',');
}
/// <summary>
/// 打印结果
/// </summary>
public void PrintResult()
{
for (int m = 0; m < 3; m++)
{
if (m == 0) Console.WriteLine("".PadLeft(25, '-'));
for (int i = 0; i < 3; i++)
{
for (int n = 0; n < 3; n++)
{
if (n == 0) Console.Write("| ");
for (int j = 0; j < 3; j++)
{
var pv = pArr[m][n][i][j];
if (pv.Default) Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0} ", pv.Value);
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write("| ");
}
Console.WriteLine();
}
Console.WriteLine("".PadLeft(25, '-'));
}
}
private Number[][][][] GetArray(int[] defVal)
{
Number[][][][] pArr = new Number[3][][][];
for (int m = 0; m < 3; m++)
{
pArr[m] = new Number[3][][];
for (int n = 0; n < 3; n++)
{
pArr[m][n] = new Number[3][];
for (int i = 0; i < 3; i++)
{
pArr[m][n][i] = new Number[3];
for (int j = 0; j < 3; j++)
{
pArr[m][n][i][j] = new Number() { Value = 0, Default = false };
}
}
}
}
int c = 0;
for (int m = 0; m < 3; m++)
{
for (int i = 0; i < 3; i++)
{
for (int n = 0; n < 3; n++)
{
for (int j = 0; j < 3; j++)
{
int val = defVal[c++];
pArr[m][n][i][j].Value = val;
if (val > 0)
pArr[m][n][i][j].Default = true;
}
}
}
}
return pArr;
}
private void FillArray()
{
for (int m = 0; m < 3; m++)
{
for (int n = 0; n < 3; n++)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (pArr[m][n][i][j].Default == false)
{//空白格
var pv = GetNumber(new int[4] { m, n, i, j });
if (!pv.Ok)
{
m = pv.Position[0];
n = pv.Position[1];
i = pv.Position[2];
j = pv.Position[3];
ClearAfter(pv.Position);
}
pArr[m][n][i][j].Value = pv.Value;
}
}
}//end small
}
}//end big
}
//清空该位置后边的空格
private void ClearAfter(int[] pos)
{
int min = pos[0] * 1000 + pos[1] * 100 + pos[2] * 10 + pos[3];
for (int m = 0; m < 3; m++)
{
for (int n = 0; n < 3; n++)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (m * 1000 + n * 100 + i * 10 + j > min)
{
if (pArr[m][n][i][j].Default == false)
{
pArr[m][n][i][j].Value = 0;
}
}
}
}
}
}
}
//记录每个位置的可能数字
private List<PosValues> posVals = new List<PosValues>();
//获取当前位置的可能数字
private PosValues GetNumber(int[] pos)
{
List<int> nums = new List<int>();
for (int n = 1; n <= 9; n++)
{
nums.Add(n);
}
//3宫格内
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int a = pArr[pos[0]][pos[1]][i][j].Value;
if (a > 0 && nums.Contains(a))
{
nums.Remove(a);
}
}
}
//横向
for (int n = 0; n < 3; n++)
{
for (int j = 0; j < 3; j++)
{
int a = pArr[pos[0]][n][pos[2]][j].Value;
if (a > 0 && nums.Contains(a))
{
nums.Remove(a);
}
}
}
//纵向
for (int m = 0; m < 3; m++)
{
for (int i = 0; i < 3; i++)
{
int a = pArr[m][pos[1]][i][pos[3]].Value;
if (a > 0 && nums.Contains(a))
{
nums.Remove(a);
}
}
}
if (nums.Count == 0)
{
if (posVals.Count == 0)
{
return new PosValues()
{
Value = 0
};
}
var pv = posVals[posVals.Count - 1];
pv.Ok = false;
pv.Value = pv.Values[0];
pv.Values.Remove(pv.Value);
if (pv.Values.Count == 0)
{
posVals.Remove(pv);
}
return pv;
}
else
{
var pv = new PosValues();
pv.Position = pos;
pv.Value = nums[0];
nums.Remove(pv.Value);
pv.Values = nums;
if (nums.Count > 0)
{
posVals.Add(pv);
}
return pv;
}
}
}
public struct Number
{
public int Value;
public bool Default;
}
public class PosValues
{
public int[] Position;
public List<int> Values;
public bool Ok = true;
public int Value;
}
}