Advertisement
Guest User

Sudoku solver

a guest
Nov 29th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.83 KB | None | 0 0
  1. function [res,s] = boardSolve(board)
  2.  
  3. % Code that solves sudoku boards recursively with a brute-force algorithm.
  4.  
  5. %Checking if board is solved. Returns the board if it's been solved.
  6.     if ~ismember(board,0)
  7.         res = board;
  8.         s = 0;
  9.         return    
  10.  
  11.     else
  12.         % making a list of the coordinates of all empty points on the board
  13.         [r,c] = find(board == 0);
  14.  
  15.         empties = [r,c];
  16.  
  17.         [x,y] = size(empties);
  18.  
  19.         possibles = zeros(x,9);
  20.  
  21. %       Makes a list of possible moves corresponding to each cell in the
  22. %       variable 'empties'.
  23.  
  24.         for i = 1:x
  25.             for j = 1:9
  26.                 possibles(i,j) = validity(board,empties(i,1),empties(i,2),j);
  27.             end
  28.         end
  29.  
  30.        
  31. %       Checks if there are any possible moves at all. Returns if there
  32. %       aren't.
  33.         if ~ismember(possibles,1)
  34.             res = board;
  35.             s = 0;
  36.             return
  37.         end
  38.  
  39. %       Starts a for-loop that goes through every single legal placement
  40. %       of numbers. One number is placed on the board and then this
  41. %       function is called recursively with the new board.
  42.  
  43.         for i = 1:x
  44.             for j = 1:9
  45.                 if possibles(i,j) == 1
  46.                     if ~ismember(board,0)
  47.                         res = board;
  48.                         s = 0;
  49.                         return
  50.                     else
  51.                         board(empties(i,1),empties(i,2)) = j;
  52.                         [res,s] = boardSolve(board);
  53.                     end
  54.                 end
  55.             end
  56.         end
  57.  
  58.     end
  59. end
  60.  
  61.  
  62. %Checks the validity of a placement
  63. function valid = validity(board,row,col,val)
  64.  
  65.     valid = 1;
  66.  
  67.  
  68.     %Determines which region of the board a cell is in.
  69.     q = squareFind(row,col);
  70.    
  71.     if val == 0
  72.         valid = 1;
  73.     elseif ismember(val,board(row,:)) || ismember(val,board(:,col))
  74.         valid = 0;
  75.     elseif ismember(val,board(q(1,:),q(2,:)))
  76.         valid = false;
  77.     end
  78.  
  79. end
  80.  
  81. %Determines which region of the board a cell is in.
  82. function mat = squareFind(row,col)
  83.  
  84.     if row<=3
  85.         if col<=3
  86.             x = 1;
  87.             y = 1;
  88.         elseif (col <=6)
  89.             x = 1;
  90.             y = 4;
  91.         else
  92.             x = 1;
  93.             y = 7;
  94.         end
  95.     elseif row <=6
  96.         if col<=3
  97.             x = 4;
  98.             y = 1;
  99.         elseif (col <=6)
  100.             x = 4;
  101.             y = 4;
  102.         else
  103.             x = 4;
  104.             y = 7;
  105.         end
  106.     else
  107.         if col<=3
  108.             x = 7;
  109.             y = 1;
  110.         elseif (col <=6)
  111.             x = 7;
  112.             y = 4;
  113.         else
  114.             x = 7;
  115.             y = 7;
  116.         end
  117.     end
  118.          
  119.     [start] = [x,y];
  120.    
  121.    
  122.     mat = [start(1):start(1)+2;start(2):start(2)+2];
  123.    
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement