经过实际测试,在解决高难度数独时,解题器的效果仍旧不理想,所以添加了数字提示功能。
另外添加了自定义数独数据功能。
MainWindow.xaml
<Window x:Class="SudokuSolver.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:SudokuSolver"mc:Ignorable="d"Title="SudokuSolver" Height="450" Width="600"><Grid><UniformGrid Name="InputGrid" Height="360" Rows="9" Columns="9" Margin="40,30,185,20"/><UniformGrid Name="BorderPanel" Height="360" Rows="3" Columns="3" Margin="40,30,185,20"/><Button Name="SolveBtn" Height="50" Width="100" Margin="430,70,40,290" Content="OneStep"/><Button Name="RestartBtn" Height="50" Width="100" Margin="430,130,40,190" Content="Restart"/><Button Name="CustomBtn" Height="50" Width="100" Margin="430,210,40,110" Content="Clear"/><Button Name="SaveBtn" Height="50" Width="100" Margin="430,300,40,50" Content="Save"/><Button Name="Tips" Height="50" Width ="50" Content="Tips" Margin="520,10,10,340"/></Grid></Window>
MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace SudokuSolver
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{private int[,] numArr = new int[9, 9];private List<TextBox> textBoxList = new List<TextBox>();private struct FillNum {public int row;public int col;public List<int> possibleNums;}private bool isCustom = false;private int[,] customNumArr = new int[9, 9];private List<FillNum> tipsList = new List<FillNum>();public MainWindow(){InitializeComponent();Create81Grids();DrawBorders();InitSudokuArr();SolveBtn.Click += SolveBtn_Click;RestartBtn.Click += RestartBtn_Click;CustomBtn.Click += CustomBtn_Click;SaveBtn.Click += SaveBtn_Click;Tips.Click += Tips_Click;}private void Create81Grids() {//创建格子for (int i = 0; i < 81; i++){TextBox textBox = new TextBox();textBox.Height = 30;textBox.Width = 30;textBox.FontSize = 25;textBox.Foreground = Brushes.Black;textBox.TextAlignment = TextAlignment.Center;textBox.Name = "box_"+i.ToString();textBox.KeyDown += TextBox_KeyDown;textBoxList.Add(textBox);InputGrid.Children.Add(textBox);}}private void TextBox_KeyDown(object sender, KeyEventArgs e){TextBox textBox = sender as TextBox;textBox.Foreground = Brushes.Blue;int index = int.Parse(textBox.Name.Split('_')[1]);numArr[index / 9, index % 9] = int.Parse(textBox.Text);}private void DrawBorders() {//绘制边框for (int i = 0; i < 9; i++){Border border = new Border();border.Height = 120;border.Width = 120;border.BorderThickness = new Thickness(3, 3, 3, 3);border.BorderBrush = Brushes.Black;BorderPanel.Children.Add(border);}}private void InitSudokuArr() {//数独数据初始化numArr = new int[9,9] {{ 7, 1, 3, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 5, 0, 3 },{ 0, 0, 6, 0, 3, 8, 0, 7, 0 },{ 9, 7, 0, 0, 0, 6, 0, 0, 8 },{ 0, 0, 0, 9, 0, 2, 0, 0, 0 },{ 2, 0, 0, 1, 0, 0, 0, 5, 6 },{ 0, 5, 0, 4, 6, 0, 8, 0, 0 },{ 8, 0, 2, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 6, 1, 7 },};for (int i = 0; i < textBoxList.Count; i++)if (numArr[i / 9, i % 9] != 0)textBoxList[i].Text = numArr[i / 9, i % 9].ToString();elsetextBoxList[i].Text = "";}private void CustomBtn_Click(object sender, RoutedEventArgs e)//清除初始数据{numArr = new int[9, 9] {{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 6 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },};for (int i = 0; i < textBoxList.Count; i++)textBoxList[i].Text = "";}private void SaveBtn_Click(object sender, RoutedEventArgs e)//保存自定义初始数据{isCustom = true;for (int i = 0; i < textBoxList.Count; i++)if (textBoxList[i].Text != "")numArr[i / 9, i % 9] = int.Parse(textBoxList[i].Text);elsenumArr[i / 9, i % 9] = 0;customNumArr = numArr;}private void RestartBtn_Click(object sender, RoutedEventArgs e)//游戏还原{if (isCustom){numArr = customNumArr;for (int i = 0; i < textBoxList.Count; i++)if (numArr[i / 9, i % 9] != 0 && textBoxList[i].Foreground == Brushes.Black)textBoxList[i].Text = numArr[i / 9, i % 9].ToString();elsetextBoxList[i].Text = "";}else {for (int i = 0; i < textBoxList.Count; i++){textBoxList[i].Foreground = Brushes.Black;}InitSudokuArr();}}bool isShowTips = false;private void Tips_Click(object sender, RoutedEventArgs e){if (isShowTips){for (int i = 0; i < textBoxList.Count; i++){if (textBoxList[i].Foreground == Brushes.Red){textBoxList[i].Text = "";textBoxList[i].FontSize = 25;textBoxList[i].Foreground = Brushes.Black;}}isShowTips = false;}else{for (int i = 0; i < tipsList.Count; i++){FillNum fillNum = tipsList[i];if (fillNum.possibleNums.Count > 0){int index = fillNum.row * 9 + fillNum.col;if (textBoxList[index].Text == ""){textBoxList[index].FontSize = 10;textBoxList[index].Foreground = Brushes.Red;for (int j = 0; j < fillNum.possibleNums.Count; j++){textBoxList[index].Text += fillNum.possibleNums[j];}}}}isShowTips = true;}}private void SolveBtn_Click(object sender, RoutedEventArgs e)//点击解决按钮{CheckWrong();//找出可填数据List<FillNum> possibleNumsList = new List<FillNum>();for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){int num = numArr[i, j];if (num == 0) {FillNum fillNum = new FillNum();fillNum.row = i;fillNum.col = j;fillNum.possibleNums = FindPossibleNums(i, j);possibleNumsList.Add(fillNum);}}}tipsList = possibleNumsList;List<List<FillNum>> Grid9List = Splict9GridList(possibleNumsList);List<List<FillNum>> Row9List = Splict9RowList(possibleNumsList);List<List<FillNum>> Col9List = Splict9ColList(possibleNumsList);for (int i = 0; i < Grid9List.Count; i++){List<FillNum> gridList = Grid9List[i];//代表某个九宫格的可填数据List<FillNum> rowList = Row9List[i];List<FillNum> colList = Col9List[i];int guessNum = 1;while (guessNum < 10) {int activeCountG = 0;int activeCountR = 0;int activeCountC = 0; FillNum fillFlag = new FillNum();//判断九宫格for (int j = 0; j < gridList.Count; j++){List<int> possibleNums = gridList[j].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = gridList[j];activeCountG++;}}if (activeCountG == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);//判断行for (int k = 0; k < rowList.Count; k++){List<int> possibleNums = rowList[k].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = rowList[k];activeCountR++;}}if (activeCountR == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);//判断列for (int l = 0; l < colList.Count; l++){List<int> possibleNums = colList[l].possibleNums;if (possibleNums.Contains(guessNum)){fillFlag = colList[l];activeCountC++;}}if (activeCountC == 1)FillNumber(fillFlag.row, fillFlag.col, guessNum);guessNum++;}}}private List<int> FindPossibleNums(int row, int col) {//找出格子可填数字List<int> optionNums = new List<int>{1,2,3,4,5,6,7,8,9};CheckOneRow(row, ref optionNums);CheckOneCol(col, ref optionNums);CheckOneGrid(row, col, ref optionNums);if (optionNums.Count == 1)FillNumber(row, col, optionNums[0]);return optionNums;}private void CheckOneRow(int row, ref List<int> nums) {//检查一行for (int i = 0; i < 9; i++){int num = numArr[row, i];if (nums.Contains(num))nums.Remove(num);}}private void CheckOneCol(int col, ref List<int> nums) {//检查一列for (int i = 0; i < 9; i++){int num = numArr[i, col];if (nums.Contains(num))nums.Remove(num);}}private void CheckOneGrid(int row, int col, ref List<int> nums) {//检查九宫格int startI = row / 3 * 3;int startJ = col / 3 * 3;for (int i = startI; i < startI+3; i++){for (int j = startJ; j < startJ+3; j++){int num = numArr[i, j];if (nums.Contains(num))nums.Remove(num);}}}private void CheckWrong() {//检查异常//检查行列for (int i = 0; i < 9; i++){List<int> allNumsRow = new List<int>();List<int> allNumsCol = new List<int>();bool isRowSame = false;bool isColSame = false;int sameRowIndex = -1;int sameColIndex = -1;for (int j = 0; j < 9; j++){int numR = numArr[i, j];if (!allNumsRow.Contains(numR))allNumsRow.Add(numR);else if(numR != 0) {isRowSame = true;sameRowIndex = i+1;}int numC = numArr[j, i];if (!allNumsCol.Contains(numC))allNumsCol.Add(numC);else if (numC != 0){isColSame = true;sameColIndex = i+1;}}if (isRowSame)MessageBox.Show("Wrong: Row" + sameRowIndex);if (isColSame)MessageBox.Show("Wrong: Col" + sameColIndex);}//检查九宫格for (int i = 0; i < 9; i+=3){for (int j = 0; j < 9; j+=3){List<int> allNumsGrid = new List<int>();bool isGridSame = false;for (int x = i; x < i+3; x++){for (int y = j; y < j+3; y++){int num = numArr[x, y];if (!allNumsGrid.Contains(num))allNumsGrid.Add(num);else if(num != 0)isGridSame = true;}}if (isGridSame)MessageBox.Show("Wrong: Grid" + GetGridIndex(i,j));}}}private List<List<FillNum>> Splict9GridList(List<FillNum> orginalList) {//可能填的数字按九宫格划分List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int gridIndex = GetGridIndex(fillNum.row, fillNum.col);resList[gridIndex].Add(fillNum);}return resList;}private List<List<FillNum>> Splict9RowList(List<FillNum> orginalList)//可能填的数字按行划分{List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int rowIndex = fillNum.row;resList[rowIndex].Add(fillNum);}return resList;}private List<List<FillNum>> Splict9ColList(List<FillNum> orginalList)//可能填的数字按列划分{List<List<FillNum>> resList = new List<List<FillNum>>();for (int i = 0; i < 9; i++){resList.Add(new List<FillNum>());}for (int i = 0; i < orginalList.Count; i++){FillNum fillNum = orginalList[i];int colIndex = fillNum.col;resList[colIndex].Add(fillNum);}return resList;}private int GetGridIndex(int i, int j) {//获取所在九宫格索引if (i < 3 && j < 3) return 0;else if (i < 3 && j < 6) return 1;else if (i < 3) return 2;else if (i < 6 && j < 3) return 3;else if (i < 6 && j < 6) return 4;else if (i < 6) return 5;else if (j < 3) return 6;else if (j < 6) return 7;else return 8;}private void FillNumber(int i, int j, int num) {//填充数据numArr[i,j] = num;textBoxList[i * 9 + j].Foreground = Brushes.Green;textBoxList[i * 9 + j].Text = num.ToString();//MessageBox.Show((i + 1) + "行" + (j + 1) + "列解决:" + num);}}
}
以下是实际效果截图:
点击Clear按钮可以清除原始数据,然后用户输入数据,点击Save保存数据。
点击Tips按钮可以显示红色提示文本,点击第二次可以隐藏。
另外优化了求解算法,可以再更少的步骤得出结果。