Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. // return type will be a vector<int>
  2. vector<int> nextLegalPosition(vector<int> board, int n){
  3. // If the given position is not legal, find the next legal position in the row. If there is not, find one in the next row. Or need to backtrack? That would kinda make more sense.
  4. // TODO: Could go ask TA about that.
  5.  
  6. printBoard(board);
  7. vector<int> lastPosition = findLastPosition(board);
  8.  
  9. if(findLastPosition(board)[0] == board.size()){
  10. // the board is full.
  11. cout << "The board is full!";
  12. // If the position is not legal, look for another. If you can't find one, backtrack.
  13. cout << "\np1: " << findLastPosition(board)[0] << "\n";
  14. cout << "\np2: " << findLastPosition(board)[1] << "\n";
  15. cout << "\nBoardsize" << board.size() << "\n";
  16. //cout << "board" << board[7];
  17. if(!isLegalPosition(board)){
  18. // This one can be less than, we don't need to check the earlier ones, we know they already are.
  19. for(int col = findLastPosition(board)[1]; col < board.size(); col++){
  20. vector<int> newBoard = board;
  21. cout << "\nlast col: " << col << "\n";
  22. newBoard[board.size()] = col;
  23. if(isLegalPosition(newBoard)){
  24. cout << "this find last position board 1 works!\n";
  25. printBoard(newBoard);
  26. return newBoard;
  27. }
  28. }
  29. vector<int> newBoard = board;
  30. newBoard[lastPosition[0]] = findLastPosition(board)[1];
  31. backtrackLegalSolution(newBoard, lastPosition[0] - 1);
  32. }
  33. else if(isLegalPosition(board)){
  34. cout << "el board " << board[board.size() - 1] << " - " << findLastPosition(board)[1] << "\n";
  35. if(findLastPosition(board)[1] == board[board.size() - 1]){
  36. cout << "at edge\n";
  37. }
  38. else{
  39. for(int col = findLastPosition(board)[1]; col < board.size(); col++){
  40. vector<int> newBoard = board;
  41. cout << "\nlast col 2: " << col;
  42. newBoard[board.size()] = col;
  43. if(isLegalPosition(newBoard)){
  44. cout << "this find last position board 2 works!\n";
  45. printBoard(newBoard);
  46. return newBoard;
  47. }
  48. }
  49. }
  50. vector<int> newBoard = board;
  51. newBoard[lastPosition[0]] = 0;
  52. backtrackLegalSolution(newBoard, lastPosition[0] - 1);
  53. }
  54. }
  55. else if(!isLegalPosition(board)){
  56. cout << "Last position val: " << lastPosition[1] << "\n";
  57. // lastPosition[0] is the index
  58. for(int col = 1; col < board.size() + 1; col++){
  59. vector<int> newBoard = board;
  60. cout << "col: " << col;
  61. // newBoard at the row (lastPosition[0]), is equal to the value of the column.
  62. newBoard[lastPosition[0]] = col;
  63. if(isLegalPosition(newBoard)){
  64. cout << "\nThis board works\n";
  65. printBoard(newBoard);
  66. return newBoard;
  67. }
  68.  
  69. }
  70. vector<int> newBoard = board;
  71. // TODO: Do I need to change this to one?
  72. newBoard[lastPosition[0] - 1] = board[lastPosition[0] - 1] + 1;
  73. // If the number is eight, meaning all other things have been checked in this, look for another solution.
  74. if(!(board[lastPosition[0] - 1] == board.size())){
  75. newBoard[lastPosition[0] - 1] = 0;
  76. }
  77. newBoard[lastPosition[0]] = 0;
  78. backtrackLegalSolution(newBoard, lastPosition[0]);
  79.  
  80.  
  81. }
  82. // TODO: Need to figure out what is happening with a full board. I have partial boards working, I think.
  83. // need to figure out if a board is finished, maybe if findLastPosition[0] is the size of the board, I would have to check this first.
  84. else if(isLegalPosition(board)){
  85. // Need to go down a row now, in addition to the column.
  86. int nextRow = lastPosition[0] + 1;
  87. // TODO: There has to be a more efficient way than checking these possibilities
  88. for(int col = 1; col < board.size() + 1; col++){
  89. vector<int> newBoard = board;
  90. cout << "col2: " << col;
  91. // newBoard at the row (lastPosition[0]), is equal to the value of the column.
  92. newBoard[nextRow] = col;
  93. if(isLegalPosition(newBoard)){
  94. cout << "\nThis board works 2\n";
  95. printBoard(newBoard);
  96. return newBoard;
  97. }
  98. }
  99. vector<int> newBoard = board;
  100.  
  101. newBoard[lastPosition[0] - 1] = board[lastPosition[0] - 1] + 1;
  102. // If the number is eight, meaning all other things have been checked in this, look for another solution.
  103. if(!(board[lastPosition[0] - 1] == board.size())){
  104. newBoard[lastPosition[0] - 1] = 0;
  105. }
  106. newBoard[lastPosition[0]] = 0;
  107. backtrackLegalSolution(newBoard, lastPosition[0]);
  108.  
  109. }
  110. return {0};
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement