/// <summary> /// Attempt to solve the given nonogram /// </summary> /// <param name="ng">Nonogram to solve</param> /// <returns>Number of resolved tiles</returns> public int Run(Nonogram ng) { _ng = ng; _resultStack = new List <Result>(); _solved = false; _grid = new bool?[ng.Height][]; for (int i = 0; i < ng.Height; i++) { _grid[i] = new bool?[ng.Width]; for (int j = 0; j < ng.Width; j++) { if (ng.Resolved(i, j)) { _grid[i][j] = ng.IsTrue(i, j); } else { _grid[i][j] = null; } } } Stopwatch sw = Stopwatch.StartNew(); int resolved = Treesolve(0, 0); sw.Stop(); _benchTime = sw.Elapsed; if (resolved == -1) { return(0); } _solved = true; for (int i = 0; i < ng.Height; i++) { for (int j = 0; j < ng.Width; j++) { if (!ng.Resolved(i, j) && _grid[i][j].HasValue) { _resultStack.Push(new Result(i, j, _grid[i][j].Value)); } } } return(resolved); }
private bool CheckNonogram(bool[][] jArr, Nonogram ng) { for (int i = 0; i < jArr.Length; i++) { for (int j = 0; j < jArr[0].Length; j++) { if (!ng.Resolved(i, j) || jArr[i][j] != ng.IsTrue(i, j)) { return(false); } } } return(true); }
/// <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); }
/// <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); }
public void ResolvedTest() { Assert.AreEqual(false, ng.Resolved(0, 0)); }