码迷,mamicode.com
首页 > 其他好文 > 详细

数独解法(C#)

时间:2014-06-28 18:52:16      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   get   os   

未完成,回家继续

bubuko.com,布布扣
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 = 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

 

数独解法(C#),布布扣,bubuko.com

数独解法(C#)

标签:style   blog   http   color   get   os   

原文地址:http://www.cnblogs.com/ornithopter/p/3797248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!