Exemplo n.º 1
0
        // BACKTRACKING - posledna moznost
        // otazka - treba ho? urcite mam zachytene vsetky moznosti
        // riesenia sudoku??
        private void backtracking()
        {
            // najde prvy volny tah a vyberie prvu moznost
            // a riesi sudoku normalne
            Stack <SudokuField[, ]> state     = new Stack <SudokuField[, ]>();
            Stack <FieldBacktrack>  positions = new Stack <FieldBacktrack>();
            FieldBacktrack          pos       = findFirstPosition();

            while (IsSolved == false)
            {
                SudokuField[,] saved = deepCopy();
                state.Push(saved);
                positions.Push(pos);

                // nastavi ziadanu moznost vyberu
                try
                {
                    setMark(pos.Row, pos.Col, pos.getPointingMark(fields), false);
                }
                catch (IndexOutOfRangeException)
                {
                    break;
                }
                sudoku();

                // sudoku musi byt ciste
                if (isDirty())
                {
                    restoreState(state.Pop());
                    pos = positions.Pop();
                    pos.increment(fields);
                }
            }
            positions.Clear();
            state.Clear();
        }