Guest User

Untitled

a guest
Jul 13th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.56 KB | None | 0 0
  1. function [ puzzle, didSolve, backCount ] = solve_from_here( puzzle,row,col )
  2. %UNTITLED2 Summary of this function goes here
  3. %   Detailed explanation goes here
  4.  
  5.  
  6.  
  7.  % Set the backtracking count to 0.
  8.   backCount = 0;
  9.  
  10.  
  11.  
  12.     % If the row and column values indicate we've finished the puzzle,
  13.     %   set didSolve to true and return early from the function.  We're done.
  14.    
  15.     if row==10 && col==1
  16.         didSolve = true;
  17.         return
  18.     end
  19.    
  20.     % Compute the next row and next column positions, and store them in
  21.     %   variables.  This way, whenever we need these values, we can just
  22.     %   use the variables.  (Don't change row and col, use other variables.)
  23.  
  24.     nextRow= row;
  25.     nextCol= col + 1;
  26.     if nextCol==10
  27.         nextCol= 1;
  28.         nextRow= row+1;
  29.     end
  30.    
  31.     % If this square in the puzzle already contains a default value, we shouldn't
  32.     %   change it.  Instead, just recursively call solve_from_here, save its
  33.     %   returned values in our output variables, and return.  (We don't need to
  34.     %   do any real work, but we do need to try to solve the rest of the puzzle
  35.     %   and send the results back to our caller.)
  36.     if puzzle(row,col) ~= 0
  37.         [puzzle, didSolve, bc] = solve_from_here(puzzle,nextRow,nextCol);
  38.         backCount=backCount+bc;
  39.         return
  40.     end
  41.     % Loop through guesses 1..9
  42.     for i= 1:9
  43.        
  44.         % If the current guess can legally go in the puzzle at row, col
  45.        
  46.         if is_number_ok_here(puzzle,row,col,i)
  47.            
  48.         % Put the guess in the puzzle at row, col.
  49.             puzzle(row,col) = i;
  50.         % Recursively solve the puzzle from the next position.  Save the
  51.         %   returned values in variables, and add the returned backtracking
  52.         %   count to our backtracking count.
  53.             [puzzle, didSolve, bc] = solve_from_here(puzzle,nextRow,nextCol);
  54.             backCount=backCount+bc;
  55.         % If the recursive call solved the puzzle, set the output variables
  56.         %   correctly and return early from the function.  We're done.
  57.             if didSolve==true;
  58.                 return
  59.             else
  60.         % The guess was wrong.  Remove it from the puzzle and increment
  61.         %   our backtracking count.
  62.                 puzzle(row,col)=0;
  63.                 backCount=backCount+1;
  64.             end
  65.         end
  66.     end
  67.  
  68.  
  69.     % After the loop ends, we've made all possible guesses, but no solution
  70.     %   was found.  Set didSolve to false, and return the puzzle and
  71.     %   backtracking count as well.
  72.  
  73.  
  74.     didSolve=false;
  75.     return;
  76.  
  77. end
Add Comment
Please, Sign In to add comment