⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycell.cs

📁 Sudoku as a CSP: Using algorithms and techniques from CSP to solve an NxN Sudoku puzzle.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace MySudoku
{
    /// <summary>
    /// ...
    /// </summary>
    class MyCell
    {
        public string Value;
        public List<Pos>[]  Units;
        public List<Pos>  Peers;
        public bool assigned;

        #region Constractors :  
        /// <summary>
        /// Initializes a new instance of the <see cref="MyCell"/> class.
        /// </summary>
        /// <param name="cell">The cell.</param>
        public MyCell(MyCell cell)
        {
            Value = cell.Value;
            Units = new List<Pos>[3];
            Peers = new List<Pos>();
            assigned = cell.assigned;

            for (int i = 0; i < 3; i++)
            {
                Units[i] = new List<Pos>();
                for (int j = 0; j < cell.Units[i].Count; j++)
                    Units[i].Add(cell.Units[i][j]);
            }

            for (int i = 0; i < cell.Peers.Count; i++)
                Peers.Add(cell.Peers[i]);

        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MyCell"/> class.
        /// </summary>
        /// <param name="val">The val.</param>
        /// <param name="ln">The ln.</param>
        /// <param name="col">The col.</param>
        /// <param name="gridSize">Size of the grid.</param>
        public MyCell(string val, int ln, int col, int gridSize)
        {
            Value = val;
            Peers = new List<Pos>();
            Units = new List<Pos>[3];
            
            
            assigned = (val.Length == 1);

            SetUnitsAndPeersToCell(ln, col, gridSize);

        }
        #endregion
       
        private void SetUnitsAndPeersToCell(int ln, int col, int gridSize)
        {
            for (int i = 0; i < 3; i++)
            {
                Units[i] = new List<Pos>();
            }

            SetLineUnitAndPeers(ln, col, gridSize);

            SetRowUnitAndPeers(ln, col, gridSize);

            SetSquareUnitAndPeers(ln, col, gridSize);
        }

        private void SetSquareUnitAndPeers(int ln, int col, int gridSize)
        {
            int dx = col - (col % (int)Math.Sqrt(gridSize));
            int dy = ln - (ln % (int)Math.Sqrt(gridSize));

            for (int i = dy; i < dy + (int)Math.Sqrt(gridSize); i++)
                for (int j = dx; j < dx + (int)Math.Sqrt(gridSize); j++)
                {
                    Pos p = new Pos();
                    p.ln = i;
                    p.col = j;

                    Units[2].Add(p);
                    if ((i == ln) || (j == col)) continue;
                    Peers.Add(p);

                }
        }

        private void SetRowUnitAndPeers(int ln, int col, int gridSize)
        {
            for (int i = 0; i < gridSize; i++)
            {
                Pos p = new Pos();
                p.ln = i;
                p.col = col;
                Units[1].Add(p);
                if (i == ln) continue;
                Peers.Add(p);
            }
        }

        private void SetLineUnitAndPeers(int ln, int col, int gridSize)
        {
            for (int i = 0; i < gridSize; i++)
            {
                Pos p = new Pos();
                p.ln = ln;
                p.col = i;

                Units[0].Add(p);
                if (i == col) continue;
                Peers.Add(p);
            }
        }
       
    

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -