/// <summary> /// 挖洞操作 /// </summary> private void EmptyMatrix() { avaFull = new Avails(81, false); for (int i = 0; i < empty;) { for (int j = 0; j < avaFull.Count; j++) { int v = avaFull[j] - 1; int r = v / 9; int c = v % 9; int t = matrix[r, c]; this[r, c] = 0; avaFull.Remove(v + 1); if (i > 3 && GetAnswerCount() > 1) { avaFull.Insert(j, v + 1); this[r, c] = t; } else { i++; break; } creating(this, EventArgs.Empty); } } }
/// <summary> /// 获取指定行列的可选序列,该序列中的值不与所在行列宫中的任何一个值冲突 /// </summary> /// <param name="r"></param> /// <param name="t"></param> /// <returns></returns> public Avails GetOptional(int r, int c) { //初始化一个随机的可选序列,随机是为了防止多次生成一样的数独 Avails ava = new Avails(9, true); //将本行出现过的值从序列中删除 for (var i = 0; i < 9; i++) { if (i == c) { continue; } if (ava.Contains(matrix[r, i])) { ava.Remove(matrix[r, i]); } } //将本列出现过的值从序列中删除 for (var i = 0; i < 9; i++) { if (i == r) { continue; } if (ava.Contains(matrix[i, c])) { ava.Remove(matrix[i, c]); } } //将本宫出现过的值从序列中删除 for (int i = r / 3 * 3; i < r / 3 * 3 + 3; i++) { for (int j = c / 3 * 3; j < c / 3 * 3 + 3; j++) { if (i == r && j == c) { continue; } if (ava.Contains(matrix[i, j])) { ava.Remove(matrix[i, j]); } } } return(ava); }