コード例 #1
0
ファイル: SudokuGrid.cs プロジェクト: TripleStep/SudokuSolver
        public void SetDefaultKiller()
        {
            int      cells    = Cells;
            CageInfo cageInfo = new CageInfo(cells);
            int      numCages = 0;

            int[] totals = new int[cells * cells];
            for (int i0 = 0; i0 < CellA; ++i0)
            {
                for (int i1 = 0; i1 < CellB; ++i1)
                {
                    for (int i2 = 0; i2 < CellA; ++i2)
                    {
                        for (int i3 = 0; i3 < CellB; ++i3)
                        {
                            int n = (i0 * CellB + i1 + i2 + i3 * CellA) % cells;
                            int x = i0 * CellB + i1;
                            int y = i2 * CellB + i3;

                            int this_cage = numCages++;
                            totals[this_cage]    = 1 + n;
                            cageInfo.cages[x, y] = this_cage;
                        }
                    }
                }
            }
            cageInfo.totals = totals;
            this.cageInfo   = cageInfo;
        }
コード例 #2
0
ファイル: SudokuGrid.cs プロジェクト: TripleStep/SudokuSolver
 public void ClearGrid(GridOptions options)
 {
     this.cageInfo    = null;
     this.gridOptions = options;
     values           = new int[Cells, Cells];
     flags            = new CellFlags[Cells, Cells];
     for (int x = 0; x < Cells; ++x)
     {
         for (int y = 0; y < Cells; ++y)
         {
             values[x, y] = -1;
             flags[x, y]  = CellFlags.Free;
         }
     }
     solver = new SudokuSolver(this);
 }
コード例 #3
0
ファイル: SudokuGrid.cs プロジェクト: TripleStep/SudokuSolver
        public bool SetGridStrings9x9Killer(string[] a0)
        {
            GridOptions options;
            int         i0;

            CheckHeader(a0, out i0, out options);
            options.isKiller = true;
            int           cells = options.Cells;
            List <string> l     = new List <string>();
            List <string> aLine = new List <string>();
            List <string> aSum  = new List <string>();

            for (int i = i0; i < a0.Length; ++i)
            {
                string[] a = a0[i].Split(' ');
                foreach (string s in a)
                {
                    if (s.Length > 2 && s[1] == '=')
                    {
                        aSum.Add(s);
                    }
                    else if (s.Length == cells)
                    {
                        aLine.Add(s);
                    }
                    else if (s.Length > 0)
                    {
                        return(false);
                    }
                }
            }
            if (aLine.Count != options.Cells)
            {
                return(false);
            }
            CageInfo cageInfo             = new CageInfo(cells);
            Dictionary <char, int> names  = new Dictionary <char, int>();
            Dictionary <int, int>  totals = new Dictionary <int, int>();
            int num_cages = 0;

            for (int y = 0; y < cells; ++y)
            {
                string s = aLine[y];
                for (int x = 0; x < cells; ++x)
                {
                    int  this_cage;
                    char c = s[x];
                    if (c >= '1' && c <= '0' + cells)
                    {
                        this_cage         = num_cages++;
                        totals[this_cage] = c - '0';
                    }
                    else
                    {
                        if (!names.TryGetValue(c, out this_cage))
                        {
                            names[c] = this_cage = num_cages++;
                        }
                    }
                    cageInfo.cages[x, y] = this_cage;
                }
            }
            cageInfo.totals = new int[num_cages];
            foreach (string s in aSum)
            {
                int tot;
                if (!int.TryParse(s.Substring(2), out tot))
                {
                    return(false);
                }
                char c = s[0];
                int  this_cage;
                if (!names.TryGetValue(c, out this_cage))
                {
                    return(false);
                }
                totals[this_cage] = tot;
            }

            if (totals.Count != num_cages)
            {
                return(false);
            }
            cageInfo.totals = new int[num_cages];
            int grand = 0;

            for (int j = 0; j < num_cages; ++j)
            {
                cageInfo.totals[j] = totals[j];
                grand += cageInfo.totals[j];
            }
            if (grand * 2 != cells * cells * (cells + 1))
            {
                return(false);
            }

            ClearGrid(options);
            this.cageInfo = cageInfo;
            Setup();
            return(true);
        }