首页 > 代码库 > 数独解法(C#)

数独解法(C#)

未完成,回家继续

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Algorithems{    class Sudoku_Chain : IAlgorithm    {        class Blank        {            public long Possibilies = 0x111111111;            public int X;            public int Y;            public Blank LeftChain;            public Blank UpChain;            private string DisplayString            {                get                {                    return String.Format("[{0},{1}]{2:x9}", X, Y, Possibilies);                }            }            public Blank(int x, int y)            {                X = x;                Y = y;            }            public void Update(long pos)            {                if (pos == 0)                    return;                Possibilies = Possibilies & (~map[pos]);            }            public override string ToString()            {                return DisplayString;            }        }        long[,] _data = http://www.mamicode.com/new long[9, 9];        List<Blank> _blanks = new List<Blank>();        static readonly long[] map = new long[]         {            0x000000000, // 0            0x000000001, // 1            0x000000010, // 2            0x000000100, // 3            0x000001000, // 4            0x000010000, // 5            0x000100000, // 6            0x001000000, // 7            0x010000000, // 8            0x100000000, // 9        };        public void Build(int[] data)        {            for (int x = 0; x < 9; x++)            {                for (int y = 0; y < 9; y++)                {                    _data[x, y] = data[x * 9 + y];                }            }        }        private void AnalyzeBlock(Blank blank)        {            var startX = blank.X / 3 * 3;            var startY = blank.Y / 3 * 3;            for (int x = startX; x < startX + 3; x++)            {                for (int y = startY; y < startY + 3; y++)                {                    blank.Update(_data[x, y]);                }            }            for (int x = 0; x < 9; x++)            {                blank.Update(_data[x, blank.Y]);            }            for (int y = 0; y < 9; y++)            {                blank.Update(_data[blank.X, y]);            }        }        private void SearchBlanks()        {            Foreach((x,y) =>                {                    if (_data[x, y] == 0)                    {                        var blank = new Blank(x,y);                        AnalyzeBlock(blank);                        _blanks.Add(blank);                    }                });        }        private void Foreach(Action<int, int> action)        {            for (int x = 0; x < 9; x++)            {                for (int y = 0; y < 9; y++)                {                    action(x, y);                }            }        }        public void Print()        {            var sb = new StringBuilder();            sb.AppendLine(" ┌────┬────┬────┐");            for (int x = 0; x < 9; x++)            {                sb.Append("");                for (int y = 0; y < 9; y++)                {                    if (_data[x, y] == 0)                    {                        sb.Append("_");                    }                    else                    {                        sb.Append(_data[x, y]);                    }                                        if (y == 2 || y == 5 || y == 8)                    {                        sb.Append("");                    }                    else                    {                        sb.Append("  ");                    }                }                sb.AppendLine();                if (x == 2 || x == 5)                {                    sb.AppendLine(" ├────┼────┼────┤");                }            }            sb.AppendLine(" └────┴────┴────┘");            Console.WriteLine(sb.ToString());        }        public void ShowExample()        {            var sudoku = new[]            {              /*0  1  2  3  4  5  6  7  8*/                1, 0, 6, 0, 5, 9, 3, 0, 0, // 0                2, 9, 0, 1, 0, 0, 0, 5, 0, // 1                3, 0, 3, 0, 4, 0, 0, 0, 9, // 2                4, 1, 0, 8, 0, 2, 0, 0, 0, // 3                5, 4, 0, 0, 3, 0, 9, 0, 0, // 4                6, 2, 0, 0, 0, 1, 0, 6, 0, // 5                7, 0, 8, 0, 0, 0, 6, 0, 2, // 6                8, 0, 0, 4, 0, 0, 0, 8, 0, // 7                9, 0, 0, 0, 7, 8, 5, 0, 1, // 8            };            Build(sudoku);            //Print();            SearchBlanks();            _blanks.ToArray();        }    }}
View Code