Пример #1
0
        /// <summary>
        /// Attempts to determine the value of specified tile and derivates.
        /// </summary>
        /// <param name="ng">Nonogram to solve</param>
        /// <param name="row">Row index of tile</param>
        /// <param name="column">Column index of tile</param>
        /// <returns>Number of solved tiles or -1 if a contradiction was encountered</returns>
        public int Run(Nonogram ng, int row, int column)
        {
            _benchTime  = TimeSpan.Zero;
            _solved     = false;
            _resultList = new List <Result>();
            Nonogram loc = ng.Copy();

            loc.Set(row, column, true);
            // Tries solving with a value of true.
            LineSolver tSolver = new LineSolver();

            bool[] rb = new bool[loc.Height];
            rb[row] = true;
            bool[] cb = new bool[loc.Width];
            cb[column] = true;
            int tRes = tSolver.Run(loc, rb, cb);

            loc.Set(row, column, false);
            // Tries solving with a value of false.
            LineSolver fSolver = new LineSolver();

            rb         = new bool[loc.Height];
            rb[row]    = true;
            cb         = new bool[loc.Width];
            cb[column] = true;
            int fRes = fSolver.Run(loc, rb, cb);

            _benchTime = _benchTime.Add(tSolver.BenchTime()).Add(fSolver.BenchTime());
            // Determines what to return
            if (fRes == -1 || tRes == -1)
            {
                if (fRes == -1 && tRes == -1)
                {
                    return(-1);
                }
                if (fRes == -1)
                {
                    _resultList.Add(new Result(row, column, true));
                    AddRes(tSolver.Results());
                    _solved = tSolver.Solved();
                }
                else
                {
                    _resultList.Add(new Result(row, column, false));
                    AddRes(fSolver.Results());
                    _solved = fSolver.Solved();
                }
            }
            return(_resultList.Count);
        }
Пример #2
0
        /// <summary>
        /// Runs the SerialSolver
        /// </summary>
        /// <param name="ng">Nonogram to solve</param>
        /// <returns>Number of solver tiles or -1 if a contradiction was encountered</returns>
        public int Run(Nonogram ng)
        {
            _ng        = ng.Copy();
            _benchTime = TimeSpan.Zero;
            _results   = new List <Result>();
            LineSolver ls = new LineSolver();

            if (ls.Run(_ng) > 0)
            {
                if (ls.Solved())
                {
                    _results   = ls.Results();
                    _benchTime = ls.BenchTime();
                    _solved    = true;
                    return(_results.Count);
                }
            }
            _benchTime = ls.BenchTime();
            Stopwatch sw = Stopwatch.StartNew();

            _th = new TileHeap(_ng.Width, ng.Height);
            Update(ls.Results());
            for (int i = 0; i < _ng.Width; i++)
            {
                _th.Add(0, i, _ng.GetPrio(0, i));
                _th.Add(_ng.Height - 1, i, _ng.GetPrio(_ng.Height - 1, i));
            }
            for (int i = 1; i < _ng.Height - 1; i++)
            {
                _th.Add(i, 0, _ng.GetPrio(i, 0));
                _th.Add(i, _ng.Width - 1, _ng.GetPrio(i, _ng.Width - 1));
            }
            _benchTime = _benchTime.Add(sw.Elapsed);
            TrialSolver trS = new TrialSolver();

            while (!_th.IsEmpty)
            {
                Coordinate c = _th.Poll();
                if (!_ng.Resolved(c.Row, c.Column))
                {
                    int res = trS.Run(_ng, c.Row, c.Column);
                    if (res == -1)
                    {
                        return(_results.Count);
                    }
                    _benchTime = _benchTime.Add(trS.BenchTime());
                    sw         = Stopwatch.StartNew();
                    Update(trS.Results());
                    _benchTime = _benchTime.Add(sw.Elapsed);
                    if (_ng.LeftToClear == 0)
                    {
                        _solved = true;
                        return(_results.Count);
                    }
                }
            }
            if (_ng.LeftToClear < 201 || !_smalltree)
            {
                ISolver ts = new TreeSolver();
                ts.Run(_ng);
                _benchTime = _benchTime.Add(ts.BenchTime());
                Update(ts.Results());
                _solved = true;
            }
            return(_results.Count);
        }