C# 数独 解法
2021/12/18 22:50:18
本文主要是介绍C# 数独 解法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
按照人的思维来做的。
MyPos 就是 数组 行 列值。
public class CalcShuDu { public int[][] IArrSd; public int IBlankCount; public Dictionary<MyPos, List<int>> DicCanFill; public Dictionary<MyPos, List<int>> DicFilling; Dictionary<MyPos, int> dic = new Dictionary<MyPos, int>(); public CalcShuDu(int[][] arr) { IArrSd = arr; for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr[i].Length; j++) { if (arr[i][j] == 0) { IBlankCount++; MyPos p = new MyPos(i, j); dic.Add(p, 0); } } } } public Dictionary<MyPos, int> GetAnswer() { DicFilling = new Dictionary<MyPos, List<int>>(); GetCanFill(); var keys = dic.Keys.ToList(); int fillCount = 0; bool isAdd = false; while (fillCount<= IBlankCount-1) { isAdd = false; if (fillCount < 0) { return dic; } var key = keys[fillCount]; if (DicFilling[key].Count>= DicCanFill[key].Count) { DicFilling[key].Clear(); IArrSd[key.X][key.Y] = 0; fillCount -= 1; key= keys[fillCount]; IArrSd[key.X][key.Y] = 0; continue; } var listCanFill = GetCanFill(key); if (listCanFill.Count == 0) { IArrSd[key.X][key.Y] = 0; fillCount -= 1; key = keys[fillCount]; IArrSd[key.X][key.Y] = 0; } else { foreach (var item in listCanFill) { if (!DicFilling[key].Contains(item)) { IArrSd[key.X][ key.Y] = item; DicFilling[key].Add(item); fillCount += 1; dic[key] = item; isAdd = true; break; } } if (!isAdd) { IArrSd[key.X][key.Y] = 0; DicFilling[key].Clear(); fillCount -= 1; key=keys[fillCount]; IArrSd[key.X][key.Y] = 0; } } } return dic; } public void GetCanFill() { DicCanFill = new Dictionary<MyPos, List<int>>(); var keys = dic.Keys.ToList(); foreach (var key in keys) { List<int> list1 = new List<int>(); DicFilling.Add(key, list1); List<int> list = new List<int>(); int i = key.X; int j = key.Y; var arr = IArrSd[i]; for (int k = 0; k < arr.Length; k++) { if(arr[k] != 0) { list.Add(arr[k]); } } for (int m = 0; m < 9; m++) { if (IArrSd[m][j] != 0) { if(!list.Contains(IArrSd[m][j])) list.Add(IArrSd[m][j]); } } var ninePos = GetNinePos(key); for (int m = ninePos.X; m < ninePos.X+3; m++) { for (int k = ninePos.Y; k < ninePos.Y+3; k++) { if (IArrSd[m][k] != 0) { if (!list.Contains(IArrSd[m][k])) list.Add(IArrSd[m][k]); } } } list.Sort(); var lstCanFill=new List<int>(); for (int m = 1; m < 10; m++) { if (!list.Contains(m)) { lstCanFill.Add(m); } } DicCanFill.Add(key, lstCanFill); } } public MyPos GetNinePos(MyPos pos) { int i = 0; int j = 0; int x=pos.X; int y=pos.Y; if (x >= 0 && x < 3) { if (y >= 0 && y < 3) { i = 0; j= 0; } else if (y >= 3 && y < 6) { i = 3; j = 0; } else { i = 6; j = 0; } } else if (x >= 3 && x < 6) { if (y >= 0 && y < 3) { i = 0; j = 3; } else if (y >= 3 && y < 6) { i = 3; j = 3; } else { i = 6; j = 3; } } else { if (y >= 0 && y < 3) { i = 0; j = 6; } else if (y >= 3 && y < 6) { i = 3; j = 6; } else { i = 6; j = 6; } } return new MyPos(j,i); } public List<int>GetCanFill(MyPos key) { var list = new List<int>(); int i = key.X; int j = key.Y; var arr = IArrSd[i]; for (int k = 0; k < arr.Length; k++) { if (arr[k] != 0) { list.Add(arr[k]); } } for (int m = 0; m < 9; m++) { if (IArrSd[m][j] != 0) { if (!list.Contains(IArrSd[m][j])) list.Add(IArrSd[m][j]); } } var ninePos = GetNinePos(key); for (int m = ninePos.X; m < ninePos.X + 3; m++) { for (int k = ninePos.Y; k < ninePos.Y + 3; k++) { if (IArrSd[m][k] != 0) { if (!list.Contains(IArrSd[m][k])) list.Add(IArrSd[m][k]); } } } list.Sort(); var lstCanFill = new List<int>(); for (int m = 1; m < 10; m++) { if (!list.Contains(m)) { lstCanFill.Add(m); } } return lstCanFill; } }
这篇关于C# 数独 解法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#