4.29167

4.3 | 25 ratings Rate this file 56 Downloads (last 30 days) File Size: 1.39 KB File ID: #8083

MATLAB Sudoku Solver

by Geoff Boynton

 

21 Jul 2005 (Updated 22 Jul 2005)

Solves sudoku puzzles

| Watch this File

File Information
Description

This is a recursive program that finds all possible solutions to a given sudoku puzzle. It's not the most efficient algorithm, but the entire function has less than 20 lines.

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (29)
29 Jul 2005 Ernold Blatford  
02 Oct 2005 mycsar norlie

giler la

10 Oct 2005 Moe Zyslack

some boring this program

13 Oct 2005 Jim Chang

That is just awesome code! I wish I'd written it myself. SO COOL!

03 Nov 2005 Andrew Bliss

See "MATLAB Sodoku Solver - Improved"

15 Nov 2005 Juha Haataja

Elegant solution

03 Dec 2005 Jesper Oland

Simpler:
% % Simple Recursive Solver % Programmed by Jesper Oland % spaceplant.com % function A = sudoku(A) if ~exist('A') % use this sudoku if none is given A=[0 1 0 0 0 0 0 7 0; 3 0 2 1 0 9 8 0 4; 0 0 0 5 0 8 0 0 0; 6 0 0 0 9 0 0 0 2; 0 4 0 0 0 0 0 3 0; 8 0 0 0 3 0 0 0 7; 0 0 0 9 0 5 0 0 0; 2 0 4 7 0 3 9 0 1; 0 9 0 0 0 0 0 4 0]; end [yy xx]=find(A==0); % find empty cells if length(xx)==0 disp('solution') disp(A); return end x=xx(1); y=yy(1); for i=1:9 % try 1 to 9 y1=1+3*floor((y-1)/3); % find 3x3 block x1=1+3*floor((x-1)/3); if ~( any(A(y,: )==i) | any(A(:,x)==i) | any(any(A(y1:y1+2,x1:x1+2)==i)) ) % if valid number tmp=A; tmp(y,x)=i; tmp=sudoku(tmp); % solve with this if all(all(tmp)) A=tmp; % solution return; % done end end end

03 Dec 2005 Jesper Oland

http://www.sudoku.org.uk/discus/messages/5/105.html?1123204270

09 Jan 2006 Frank Diggelen

I love this line:
if sum(M(i,:)==k)==0 & sum(M(:,j)==k)==0 & sum(mm(:)==k)==0 %OK for column, row, and 3x3 block

very elegant. Thanks

02 Feb 2006 Warren Hall

Clean, crisp logic with a wonderful use of GUI. The solver does everything it claims with speed and precision.

09 Feb 2006 juan maria sandoval  
12 Feb 2006 Fahad Al Mahmood

Works As Advertised! Thank you Geoff!

27 Feb 2006 Kate Chen

very clver programming, but may get limitations if two many recursive step have to be taken

23 Jun 2006 Abhishek Dasgupta

Lovely bit of coding ...

02 Oct 2006 Matthew Grimshaw

Clever. I used this code with some additions (verification of the matrix, etc.) in a laboratory session teaching MATLAB to engineering students.

12 Oct 2006 J Taylor

One single factor plays on my mind...what if it makes an incorrect guess? there is no way to make any sort of correction. or am i missing something?

25 Oct 2006 praveen alladi

good,it is very helpfull

11 Nov 2006 Thomas Wichmann

Generally a nice algorithm. However, it can be fooled by providing an incorrect starting matrix. For instance, it will "solve" the obviously incorrect matrix M:
M = [0,0,1,9,0,0,1,0,8;...
6,0,0,1,8,5,1,3,0;...
0,0,7,0,6,0,1,0,0;...
0,3,4,0,9,0,0,0,0;...
0,0,0,5,0,4,0,0,0;...
0,0,0,0,1,0,4,2,0;...
0,0,5,0,7,0,9,0,0;...
0,1,1,8,4,0,0,0,7;...
7,0,0,0,0,9,2,0,2];

04 Dec 2006 Vincent Cunningham

Good program

05 Feb 2007 Peter Bone

This will solve most easy Sudokus but not all. It doesn't check that a number must go in a particular box because it can't go in any of the other boxes in the row, col, square. Even if you included that it would not solve non-deterministic problems where you have to think several moves ahead. This can be done by guessing and then going back to the previous state if a contradiction is found.

22 Feb 2007 Felix Voigt

The comment sent on 2007-02-05 is not correct. The algorithm will find ALL solutions to ALL non-contradictory Sudocu initial conditions. (Provided that your computer has enough processing power and you have enough time ...) By a recursive algorithm it consequently checks all possibilities for the 9 digits in non-occupied places of the 9x9 matrix, cancelling at configurations where two identical digits would be located in the same column or row or 3x3-square. So it finds ALL multiple solutions, in the case the initial conditions are to be solved in different ways. Wonderful.

17 Oct 2007 hi hi

chung toi la chien si

27 Apr 2008 a nilu

very good

04 May 2008 Olivier Planchon

works fine. neat and pretty code. Comments are well appreciated.

04 Sep 2008 Husam Aldahiyat

Excellent!

04 Sep 2008 Husam Aldahiyat  
11 Dec 2008 Wei Weimin

Excellent.
http://www.truemark.cn/stegano/
An Information Hiding Scheme Using Sudoku.pdf

13 Jan 2011 Ce Gao  
24 May 2011 mostafa solyman

good work

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
games Geoff Boynton 22 Oct 2008 07:53:52
sudoku solution solver Geoff Boynton 22 Oct 2008 07:53:52
soduku Geoff Boynton 22 Oct 2008 07:53:52
solutions Geoff Boynton 22 Oct 2008 07:53:52
puzzle Geoff Boynton 22 Oct 2008 07:53:52
algorithms Geoff Boynton 22 Oct 2008 07:53:52
recursive Trung Duong 10 Mar 2010 18:08:18
algorithms Alexander 24 May 2011 02:07:05
sudoku solution solver Alexander 24 May 2011 02:07:10
algorithms Pietro Campagna 11 Aug 2011 09:09:58

Contact us at files@mathworks.com