Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 public void SetAndClearTest()
 {
     ng.Set(4, 3, true);
     Assert.AreEqual(true, ng.IsTrue(4, 3));
     Assert.AreEqual(true, ng.Resolved(4, 3));
     Assert.AreEqual(19, ng.LeftToClear);
     ng.Set(4, 3, false);
     Assert.AreEqual(false, ng.IsTrue(4, 3));
     Assert.AreEqual(true, ng.Resolved(4, 3));
     Assert.AreEqual(19, ng.LeftToClear);
     ng.Clear(4, 3);
     Assert.AreEqual(false, ng.IsTrue(4, 3));
     Assert.AreEqual(false, ng.Resolved(4, 3));
     Assert.AreEqual(20, ng.LeftToClear);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Updates this solver with results from another solver
 /// </summary>
 /// <param name="resQueue">results from another solver</param>
 private void Update(List <Result> resQueue)
 {
     while (!resQueue.IsEmpty)
     {
         Result res = resQueue.Dequeue();
         _ng.Set(res.Row, res.Column, res.State);
         UpdatePrioHeap(res.Row, res.Column);
         _results.Push(res);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluates the specified column
        /// </summary>
        /// <param name="index">column index</param>
        /// <returns>False if a contradiction is encountered</returns>
        private bool FillColumn(int index)
        {
            bool solved = true;

            for (int i = 0; i < _ng.Height; i++)
            {
                if (!_ng.Resolved(i, index))
                {
                    solved = false;
                    break;
                }
            }
            if (solved)
            {
                return(true);
            }
            int[] leftInts = new int[_ng.Height];
            if (!ColLeft(index, leftInts, 0, 0))
            {
                return(false);
            }
            int[] rightInts = new int[_ng.Height];
            ColRight(index, rightInts, _ng.GetColumnArray(index).Length - 1, _ng.Height - 1);
            for (int i = 0; i < leftInts.Length; i++)
            {
                if (leftInts[i] == rightInts[i] && !_ng.Resolved(i, index))
                {
                    bool state = leftInts[i] % 2 == 0;
                    _rowChanged[i]     = true;
                    _colChanged[index] = true;
                    _resultList.Add(new Result(i, index, state));
                    _ng.Set(i, index, state);
                }
            }
            return(true);
        }