Beispiel #1
0
        public void solve(int n)
        {
            //ищем свободную клетку:
            Point newCell = FindNextCell();
            int   c       = (int)newCell.X;
            int   r       = (int)newCell.Y;

            for (int num = 1; num <= 9; ++num)
            {
                //ищем oчередную цифру:
                if (!test(c, r, num))
                {
                    continue;
                }

                //ставим в сетку:
                placeNum(c, r, num);

                if (nSolved < Grid.GetLength(0) * Grid.GetLength(1))
                {
                    solve(n + 1);
                }
                else
                {
                    ++nVar;
                    //Запоминаем последнее решение:
                    Solution = new NewCell[Grid.GetLength(0), Grid.GetLength(1)];
                    for (int j = 0; j < Grid.GetLength(1); ++j)
                    {
                        for (int i = 0; i < Grid.GetLength(0); ++i)
                        {
                            bool fix   = Grid[i, j].fix;
                            int  nm    = Grid[i, j].num;
                            bool color = Grid[i, j].color;
                            Solution[i, j] = new NewCell(nm, fix, color);
                        }
                    }
                }
                //возвращаемся назад:
                deleteNum(c, r);
            }
        }
Beispiel #2
0
        //ЗАГРУЖАЕМ ЗАДАЧУ С ДИСКА
        public NewCell[,] LoadPuzzle()
        {
            List <string> puzzle = new List <string>();

            // очищаем cписок строк:
            puzzle.Clear();

            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var filePath      = System.IO.Path.Combine(documentsPath, "task.txt");
            //загружаем задачу с диска:
            StreamReader sr = new StreamReader(filePath);
            //считываем слова:
            string s = null;

            while ((s = sr.ReadLine()) != null)
            {
                // заменяем точки нулями:
                s = s.Replace('.', '0');
                puzzle.Add(s);
            }
            sr.Close();
            sr = null;

            int CellCol = 0, CellRow = 0;

            // в первой строке - размеры сетки:
            string[] str2 = puzzle[0].Split(new char[] { ' ', '-', 'x' });
            int      n    = 0;

            if (int.TryParse(str2[0], out n))
            {
                CellCol = n;
            }
            if (int.TryParse(str2[1], out n))
            {
                CellRow = n;
            }

            // очищаем список блоков:
            lstBlocks.Clear();
            // создаем пустую сетку:
            Grid    = new NewCell[CellCol, CellRow];
            nSolved = 0;

            // сетка - строки 1 ..CellRow:
            for (int i = 1; i < CellRow + 1; ++i)
            {
                s = puzzle[i];
                int r = i - 1;

                // разбиваем строку:
                string[] asc = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                for (int ns = 0; ns < asc.Length; ++ns)
                {
                    string sc = asc[ns];
                    // определяем цвет клетки:
                    int pos = sc.IndexOf('\\');
                    // черная клетка:
                    if (pos != -1)
                    {
                        // Создаем новую клетку:
                        Grid[ns, r] = new NewCell(false);
                        // ее решать не нужно:
                        ++nSolved;
                        Point nums = extractNums(sc, pos);
                        // горизонтальный блок:
                        if (nums.X > 0)
                        {
                            Block bl = new Block(ns, r, BlockDirection.BD_HORIZ, (int)nums.X);
                            lstBlocks.Add(bl);
                        }
                        //вертикальный блок:
                        if (nums.Y > 0)
                        {
                            Block bl = new Block(ns, r, BlockDirection.BD_VERT, (int)nums.Y);
                            lstBlocks.Add(bl);
                        }
                    }
                    // белая клетка:
                    else
                    {
                        // Создаем новую клетку:
                        Grid[ns, r] = new NewCell(true);
                        // какое в ней число:
                        int num = 0;
                        if (int.TryParse(sc, out num))
                        {
                            if (num != 0)
                            {
                                Grid[ns, r].num = num;
                                Grid[ns, r].fix = true;
                                ++nSolved;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Неверный символ в строке!");
                        }
                    }
                }
            }

            // блоки
            foreach (Block bl in lstBlocks)
            {
                // коорд. черной клетки:
                int x = bl.blackX;
                int y = bl.blackY;
                // направление:
                BlockDirection bd = bl.dir;
                // число белых клеток:
                int nWhite = 0;
                if (bd == BlockDirection.BD_HORIZ)
                {
                    while (x + 1 < Grid.GetLength(0) && Grid[x + 1, y].color)
                    {
                        // добавляем блок в клетку:
                        Grid[x + 1, y].lstBlocks.Add(bl);
                        // добавляем число в множество:
                        if (Grid[x + 1, y].num != 0)
                        {
                            bl.setNums.Add(Grid[x + 1, y].num);
                        }
                        ++nWhite;
                        ++x;
                    }
                }
                else
                {
                    while (y + 1 < Grid.GetLength(1) && Grid[x, y + 1].color)
                    {
                        // добавляем блок в клетку:
                        Grid[x, y + 1].lstBlocks.Add(bl);
                        // добавляем число в множество:
                        if (Grid[x, y + 1].num != 0)
                        {
                            bl.setNums.Add(Grid[x, y + 1].num);
                        }
                        ++nWhite;
                        ++y;
                    }
                }
                bl.nWhite = nWhite;
            }
            return(Grid);
        }