From cefa63da545a797f854cf115ae8d65bfdc1e4792 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Sat, 20 Jan 2018 22:31:56 +0100 Subject: [PATCH] fixed backtrack bug in logslasher function --- Source/functions/solve/structure.cpp | 55 +++++++++++++--------------- Source/main.cpp | 4 +- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 213e9c0..062dd0e 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -140,7 +140,7 @@ bool backtrack(vector& log, Puzzle& puzzleMat) //if more pieces possible, tset piece as not logged if((log.back().PieceCollector.size())>1) { - cout << " next piece" << endl; + //unset puzzlepieces int count =0; for(int i=0;i& log, Puzzle& puzzleMat) count++; } } + cout << "removed " << count << endl; - if (count !=4) - cerr << "incorrect set set" << endl; + //remove similar in log Part myPart = *log.back().PieceCollector[0].second;//tmpsaves bad part log.back().PieceCollector.erase(log.back().PieceCollector.begin());//removes bad part from log puzzleMat.removeSimilar(log.back().PieceCollector,myPart); //removes all pieces from log that are similar to bad part //TODO remove this when further layers are added!!! - - - if(log.back().PieceCollector.size()==1) - log.back().decreaseRandomed(); - - setsolution(log,puzzleMat); - - return true; + if(log.back().PieceCollector.size()) // this checks if 'removeSimilar' has cleared entire LogElement + { + cout << " next piece" << endl; + if(log.back().PieceCollector.size()==1) + log.back().decreaseRandomed(); + setsolution(log,puzzleMat); + return true; + } } //else remove log element and backtrack once more - else + puzzleMat.removeConstrains(log.back().myCoor); //this should remove constraints from all layers + int count =0; + if((log.back().PieceCollector.size())) { - - puzzleMat.removeConstrains(log.back().myCoor); //this should remove constraints from all layers - int count =0; - if((log.back().PieceCollector.size())) + for(int i=0;iGetPartID()==log.back().PieceCollector.begin()->second->GetPartID())//sets all with partid { - if(puzzleMat.p_myBox[i]->GetPartID()==log.back().PieceCollector.begin()->second->GetPartID())//sets all with partid - { - puzzleMat.p_myBox[i]->set=false; - count++; - } + puzzleMat.p_myBox[i]->set=false; + count++; } - if (count !=4) - cerr << "incorrect set set" << endl; } - cout << " no more pieces" << endl; - - log.pop_back(); - if(!backtrack(log,puzzleMat)) - return false; - return true; + cout << "removed " << count << endl; } + cout << " no more pieces" << endl; + + log.pop_back(); + if(!backtrack(log,puzzleMat)) + return false; + return true; } diff --git a/Source/main.cpp b/Source/main.cpp index 7b5ee70..56c683c 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -15,14 +15,14 @@ int main() return 0; } - puzzleMat.createRandomBox(); + //puzzleMat.createRandomBox(); cout << "Solving Puzzle now..."; while(next(log, puzzleMat)); cout << "Done!" << endl; - //puzzleMat.resultImage(log); + puzzleMat.resultImage(log); puzzleMat.printPuzzle();