Net Core教程

C# 数独 解法

本文主要是介绍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# 数独 解法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!