Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1.  
  2.  
  3. vector<int> backtrackLegalSolution(vector<int> board, int row){
  4. // Solution we want: {1, 3, 5, 7, 0, 0, 0, 0}
  5. if(row != 0){
  6. // Can only move to the right, trying other combinations to the left does not work, we are always moving towards the right.
  7. for(int col = findLastPosition(board)[1]; col < board.size() + 1; col++){
  8. cout << " this is the column : " << col << "\n";
  9. cout << "this is the row : " << row << "\n";
  10. vector<int> newBoard = board;
  11. newBoard[row] = col;
  12. if(isLegalPosition(newBoard)){
  13. cout << "This backtracking board is found!\n";
  14. printBoard(newBoard);
  15. return newBoard;
  16. }
  17. }
  18. // TODO: Need to figure out here what to do when there has been a path already traveled.
  19. vector<int> newBoard = board;
  20. newBoard[row] = 0;
  21. backtrackLegalSolution(newBoard, row - 1);
  22. }
  23. else{
  24. if(findLastPosition(board)[1] == board.size()){
  25. cout << "if the board is in the last one, no more, end it";
  26. cout << "stop the function, this is the base case???";
  27. return {0};
  28. }
  29. else{
  30. cout << "otherwise, the next position in the first row is the current position + 1, unless it is at the edge.";
  31. vector<int> newBoard = board;
  32. newBoard[0] = findLastPosition(board)[1] + 1;
  33. cout << "new board!";
  34. return newBoard;
  35. }
  36. }
  37. return {0};
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement