C# 解数独程序 注释详细
2012-10-24 10:12
260 查看
想写这个程序源于芬兰的某数学家设计出了个号称史上最难的数独,本来想自己算算,可惜自己真的算不出来。于是便想写个程序把结果解出来~~图如下所示
源程序下载地址:
百度网盘:http://pan.baidu.com/share/link?shareid=84739&uk=1460631569
CSDN下载:http://download.csdn.net/detail/mqlccl2008/4670903
简单讲讲思路,贴一些关键代码,方便懒得下载的朋友
我把数独中每个格子设置一个关联的List,然后在list中添加1到9这9个数字。然后如果在该行、列、九宫里面有该数字的话。就从这个list中删去。
其实对于一些简单的数独,做到上面这一步也就可以了,可惜上面这个数独,按照上面的方法全部处理之后也就只能填出21个左右的数字吧(记不清了
)。剩下的就只能猜了。人脑基本不够用,电脑就方便多了。
核心递归算法
private void Jisuan(List<int>[,] L, int r, int c)
{
List<int>[,] tempList = new List<int>[9, 9];
CopyList(L, tempList);//这里重写copy方法的话更易于理解一些
if (tempList[r, c].Count != 1)
{
List<int>[,] temptempList = new List<int>[9, 9];
CopyList(tempList, temptempList);
for (int i = 0; i < tempList[r, c].Count && !GetResult; i++)
{
temptempList[r, c].Clear();
temptempList[r, c].Add(tempList[r, c][i]);
List<int>[,] temptemptempList = new List<int>[9, 9];
CopyList(temptempList, temptemptempList);
do cal(temptemptempList);
while (!checkEnd(temptemptempList));//直到count数不再改变
if (!HasCount0(temptemptempList))//不含有count为0的情况
{
//获得nextr和nextc
int nextr = 0; int nextc = 0;
GetNextRC(r, c, ref nextr, ref nextc);
if (nextr == 9 && nextc == 0)//到了最后一个节点
{
printout(temptemptempList);
GetResult = true;
return;
}
else
Jisuan(temptemptempList, nextr, nextc);
}
}
}
else//tempList[r, c].Count == 1的情况
{
//获得nextr和nextc
int nextr = 0; int nextc = 0;
GetNextRC(r, c, ref nextr, ref nextc);
if (nextr == 9 && nextc == 0)//到了最后一个节点
{
printout(tempList);
GetResult = true;
return;
}
else
Jisuan(tempList, nextr, nextc);
}
}
如果想吐槽里面函数、变量名字的话……就使劲的喷吧~~因为这个博主本人能力有限,这个程序写了很多遍才写出来,各种名字都想遍了,最后英语单词实在不够用了,就只好上拼音了。变量也就有了temptemp什么什么之类的东西。后来发现竟然代码只有这么短……
源程序里面里面注释挺多的,方便和博主一样的新手理解学习互相交流~~欢迎交流,如果喷,请轻喷~~
顺便贴个程序也没原图吧
当然了~~其他数独也能解哦~
源程序下载地址:
百度网盘:http://pan.baidu.com/share/link?shareid=84739&uk=1460631569
CSDN下载:http://download.csdn.net/detail/mqlccl2008/4670903
简单讲讲思路,贴一些关键代码,方便懒得下载的朋友
我把数独中每个格子设置一个关联的List,然后在list中添加1到9这9个数字。然后如果在该行、列、九宫里面有该数字的话。就从这个list中删去。
其实对于一些简单的数独,做到上面这一步也就可以了,可惜上面这个数独,按照上面的方法全部处理之后也就只能填出21个左右的数字吧(记不清了
)。剩下的就只能猜了。人脑基本不够用,电脑就方便多了。
核心递归算法
private void Jisuan(List<int>[,] L, int r, int c)
{
List<int>[,] tempList = new List<int>[9, 9];
CopyList(L, tempList);//这里重写copy方法的话更易于理解一些
if (tempList[r, c].Count != 1)
{
List<int>[,] temptempList = new List<int>[9, 9];
CopyList(tempList, temptempList);
for (int i = 0; i < tempList[r, c].Count && !GetResult; i++)
{
temptempList[r, c].Clear();
temptempList[r, c].Add(tempList[r, c][i]);
List<int>[,] temptemptempList = new List<int>[9, 9];
CopyList(temptempList, temptemptempList);
do cal(temptemptempList);
while (!checkEnd(temptemptempList));//直到count数不再改变
if (!HasCount0(temptemptempList))//不含有count为0的情况
{
//获得nextr和nextc
int nextr = 0; int nextc = 0;
GetNextRC(r, c, ref nextr, ref nextc);
if (nextr == 9 && nextc == 0)//到了最后一个节点
{
printout(temptemptempList);
GetResult = true;
return;
}
else
Jisuan(temptemptempList, nextr, nextc);
}
}
}
else//tempList[r, c].Count == 1的情况
{
//获得nextr和nextc
int nextr = 0; int nextc = 0;
GetNextRC(r, c, ref nextr, ref nextc);
if (nextr == 9 && nextc == 0)//到了最后一个节点
{
printout(tempList);
GetResult = true;
return;
}
else
Jisuan(tempList, nextr, nextc);
}
}
如果想吐槽里面函数、变量名字的话……就使劲的喷吧~~因为这个博主本人能力有限,这个程序写了很多遍才写出来,各种名字都想遍了,最后英语单词实在不够用了,就只好上拼音了。变量也就有了temptemp什么什么之类的东西。后来发现竟然代码只有这么短……
源程序里面里面注释挺多的,方便和博主一样的新手理解学习互相交流~~欢迎交流,如果喷,请轻喷~~
顺便贴个程序也没原图吧
当然了~~其他数独也能解哦~
相关文章推荐
- c#实现验证码功能(多种模式下分别实现验证功能)详细,带注释
- 根据Merge Sort原理, 自己实现的归并排序算法+详细注释+代码(C#,C/C++) [分享]
- c#文件操作示例带详细注释
- 单词查询程序—《C++ primer 4》源代码详细注释
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序服务器端运行详细配置参考
- C# 多线程控制控件实例(例程简单,注释详细)
- 大段程序注释C#、lisp
- Win32 SDK窗口程序代码(含详细注释)
- 5 给我们的c#程序添加注释
- 分享一个C#编写简单的聊天程序(详细介绍)
- 编写高质量代码改善C#程序的157个建议——建议153:若抛出异常,则必须要注释
- 中国象棋程序的设计与实现(十二)--棋盘绘制算法(尽管注释非常详细,完全理解仍有难度)
- qt下的时钟程序(简单美丽,继承自QWidget的Clock,用timer调用update刷新,然后使用paintEvent作画就行了,超详细中文注释)good
- 微软ping程序源代码完整版(附详细的注释)
- C#编写Windows服务程序详细步骤详解(图文)
- 分析注释都非常详细的 c++ 入门程序
- 中国象棋程序的设计与实现(十二)--棋盘绘制算法(尽管注释非常详细,完全理解仍有难度)
- C# VC VB.NET JS 注释小插件(外接程序)
- 快速傅里叶变换(FFT)的C#实现及详细注释
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序客户端运行详细配置参考