Vous êtes sur la page 1sur 8

1

How to make SUDOKU puzzle?


Abhijit Kar Gupta, kg.abhi@gmail.com

You want to make your own SUDOKU tables? Then follow the rest of the write up.
Everyday millions of people all over the world remain engrossed in solving some mathematical
tables, called SUDOKU. Now almost every newspaper or magazine devotes a space for this
puzzle for their devoted readers and problem solvers. The point is, you do not have to know
mathematics to fill up such a mathematical table, only arranging numbers 1, 2, 3,9 in the 9x9
matrix. People use brute force, some patience, sometimes some tricks to fill up the blank
squares in the Sudoku puzzle. The solution is often not easy; it depends on how the missing
blocks are placed in the matrix.
While I was wondering like others how to solve the puzzle as appeared in a newspaper, I
thought of preparing the Sudoku puzzle itself from some basic principle. And yes, there you
need some little trick for sure, a fundamental property from the mathematics of Matrix.
Once you know that, you can write an algorithm and then a computer program which will result
in making as many Sudoku matrices as you want.
So how is about the algorithm?
We start from any valid Sudoku table (a complete filled up table). We can make it by hand
taking a blank 9x9 matrix and then by following the Sudoku rules.
What are the Sudoku rules?

Each row contains numbers 1, 29 without repetition


Each column contains numbers 1, 29 without repetition
If we divide the 9x9 matrix into 3x3 blocks, it becomes a 3x3 block matrix. Each block
contains numbers 1, 29 without repetition.

Imagine, we have constructed such a matrix by hand, following all the rules as above. Then the
next task to prepare many more Sudoku matrices will be a simple trick!
We use the elementary row or column operations where we interchange two rows or two
columns which leave the property of the matrix unchanged! And yes, you can interchange
columns and rows as many times as you wish.
To prepare the puzzle, we randomly remove elements following a random number generator.
The result is what we may see as a SUDOKU puzzle as you see in newspaper s or in books.

Try the following by yourself. Start from the following matrix, for example, then do the
operations as stated above. This is written in a FORTRAN program, the program looks long but it
is actually a very straightforward one (the PROGRAM is appended below).
So here are the algorithmic steps!

Start from any Sudoku matrix (a complete filled up table that you may once construct by
hand).
Interchange any two rows or any two columns of the matrix as many times as you want.
Your new Sudoku matrix is ready
To prepare a puzzle just pick up any element in that matrix randomly (following any
random number generator) and remove that. Your puzzle is ready and now it is time to
present the matrix (with missing elements) like a table, that is your Sudoku table.

[Note: The puzzle is easy, moderate or difficult depending on how many elements you
remove and their distribution over the table.]
Now just try this!
As an example, check the following matrix, which I prepared by placing the numbers following
the Sudoku rules.

How did I find the above matrix? Simple! Consider the 1st column: 1 2 3 4 5 6 7 8 9. Then for the
2nd column, the 1st member (1,2) can be 4 as it can not be 1, 2, or 3 (In a 3x3 block there should
not be any repetition.). Then the successive members in 2nd column can be 5 6 7 8 9 and then 1
2 3. In the same consideration, you can make the 3rd column and so on.
Suppose, you interchange first two columns:

The new matrix on your right is a valid Sudoku matrix.


Here is an example:
A full SUDOKU TABLE (after interchanges between rows and between columns several times)
4
5
6
9
8
7
1
3
2

1
2
3
6
5
4
7
9
8

7
8
9
3
2
1
4
6
5

8
9
1
4
3
2
5
7
6

5
6
7
1
9
8
2
4
3

2
3
4
7
6
5
8
1
9

9
1
2
5
4
3
6
8
7

6
7
8
2
1
9
3
5
4

3
4
5
8
7
6
9
2
1

Now after randomly making some sites vacant, you arrive at the following Sudoku puzzle!

8
6
9

3
2
1
4
6

8
9
1

6
7
1

1
8

3
5
4
7

3
5
4

4
5
8
7
6
9
0
1

FORTRAN PROGRAM:
C----------------------------------------------------------------C
Making SUDOKU Matrices
C
By - Abhijit Kar Gupta, e-mail: kg.abhi@gmail.com
C----------------------------------------------------------------implicit real*8(a-h,o-z)
dimension m(9,9), ic(9), ir(9)
dimension mex(9,9)
open(1,file='matrix.d', status='unknown')
write(*,*)'How many Matrices you want?'
read(*,*)ntime
write(*,*)'No. of max. attemted changes in rows'
read(*,*)nexrow
write(*,*)'No. of max. attemted changes in columns'
read(*,*)nexcol
write(*,*)'Number of entries made vacant'
read(*,*)ngap
C
-------------C
Initial Matrix
C
-------------m(1,1)=1
m(1,2)=4
m(1,3)=7
m(1,4)=2
m(1,5)=5
m(1,6)=8
m(1,7)=3
m(1,8)=6
m(1,9)=9
m(2,1)=2
m(2,2)=5
m(2,3)=8
m(2,4)=3
m(2,5)=6
m(2,6)=9
m(2,7)=4
m(2,8)=7
m(2,9)=1
m(3,1)=3
m(3,2)=6
m(3,3)=9
m(3,4)=4
m(3,5)=7
m(3,6)=1
m(3,7)=5
m(3,8)=8
m(3,9)=2
m(4,1)=4
m(4,2)=7
m(4,3)=1
m(4,4)=5
m(4,5)=8
m(4,6)=2

5
m(4,7)=6
m(4,8)=9
m(4,9)=3
m(5,1)=5
m(5,2)=8
m(5,3)=2
m(5,4)=6
m(5,5)=9
m(5,6)=3
m(5,7)=7
m(5,8)=1
m(5,9)=4
m(6,1)=6
m(6,2)=9
m(6,3)=3
m(6,4)=7
m(6,5)=1
m(6,6)=4
m(6,7)=8
m(6,8)=2
m(6,9)=5
m(7,1)=7
m(7,2)=1
m(7,3)=4
m(7,4)=8
m(7,5)=2
m(7,6)=5
m(7,7)=9
m(7,8)=3
m(7,9)=6
m(8,1)=8
m(8,2)=2
m(8,3)=5
m(8,4)=9
m(8,5)=3
m(8,6)=6
m(8,7)=1
m(8,8)=4
m(8,9)=7
m(9,1)=9
m(9,2)=3
m(9,3)=6
m(9,4)=1
m(9,5)=4
m(9,6)=7
m(9,7)=2
m(9,8)=5
m(9,9)=8
C
C
C
C
C
C

write(1,*)'Initial Matrix'
write(1,*)'--------------------------------'
write(1,9)((m(i,j),j=1,9),i=1,9)
---------------------------------------------------------------Identifying the rows and colmns inside one 3x3 box

6
C
do i=1,3
ic(i)=1
enddo
do i=4,6
ic(i)=2
enddo
do i=7,9
ic(i)=3
enddo
do i=1,3
ir(i)=1
enddo
do i=4,6
ir(i)=2
enddo
do i=7,9
ir(i)=3
enddo
C
do itime=1,ntime
C
C
C

1
C
C
C

2
C

Interchange Rows
---------------iex=1
do while(iex.lt.nexrow)
i=9*ran2(iseed)+1
ii=9*ran2(iseed)+1
if(ir(i).ne.ir(ii))go to 1
do j=1,9
tmp=m(i,j)
m(i,j)=m(ii,j)
m(ii,j)=tmp
enddo
iex=iex+1
enddo
Interchange Columns
---------------iex=1
do while(iex.lt.nexcol)
j=9*ran2(iseed)+1
jj=9*ran2(iseed)+1
if(ic(j).ne.ic(jj))go to 2
do i=1,9
tmp=m(i,j)
m(i,j)=m(i,jj)
m(i,jj)=tmp
enddo
iex=iex+1
enddo
write(1,*)'Full MATRIX => NO. ',itime
write(1,*)'
'

7
write(1,9)((m(i,j),j=1,9),i=1,9)
write(1,*)'
'
-----------------Storing the Matrix
-----------------do i=1,9
do j=1,9
mex(i,j)=m(i,j)
enddo
enddo

C
C
C

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

do igap=1,ngap
i=9*ran2(iseed)+1
j=9*ran2(iseed)+1
mex(i,j)=0
enddo
write(1,*)'Puzzle: Randomly vacant site denoted by 0'
write(1,*)'
'
write(1,*)'------------------------------------------'
write(1,*)mex(1,1), ' | ', mex(1,2), ' | ', mex(1,3), '
mex(1,4), ' | ', mex(1,5), ' | ', mex(1,6), ' | ',
mex(1,7), ' | ', mex(1,8), ' | ', mex(1,9)
write(1,*)'------------------------------------------'
write(1,*)mex(2,1), ' | ', mex(2,2), ' | ', mex(2,3), '
mex(2,4), ' | ', mex(2,5), ' | ', mex(2,6), ' | ',
mex(2,7), ' | ', mex(2,8), ' | ', mex(2,9)
write(1,*)'------------------------------------------'
write(1,*)mex(3,1), ' | ', mex(3,2), ' | ', mex(3,3), '
mex(3,4), ' | ', mex(3,5), ' | ', mex(3,6), ' | ',
mex(3,7), ' | ', mex(3,8), ' | ', mex(3,9)
write(1,*)'------------------------------------------'
write(1,*)mex(4,1), ' | ', mex(4,2), ' | ', mex(4,3), '
mex(4,4), ' | ', mex(4,5), ' | ', mex(4,6), ' | ',
mex(4,7), ' | ', mex(4,8), ' | ', mex(4,9)
write(1,*)'------------------------------------------'
write(1,*)mex(5,1), ' | ', mex(5,2), ' | ', mex(5,3), '
mex(5,4), ' | ', mex(5,5), ' | ', mex(5,6), ' | ',
mex(5,7), ' | ', mex(5,8), ' | ', mex(5,9)
write(1,*)'------------------------------------------'
write(1,*)mex(6,1), ' | ', mex(6,2), ' | ', mex(6,3), '
mex(6,4), ' | ', mex(6,5), ' | ', mex(6,6), ' | ',
mex(6,7), ' | ', mex(6,8), ' | ', mex(6,9)
write(1,*)'------------------------------------------'
write(1,*)mex(7,1), ' | ', mex(7,2), ' | ', mex(7,3), '
mex(7,4), ' | ', mex(7,5), ' | ', mex(7,6), ' | ',
mex(7,7), ' | ', mex(7,8), ' | ', mex(7,9)
write(1,*)'------------------------------------------'
write(1,*)mex(8,1), ' | ', mex(8,2), ' | ', mex(8,3), '
mex(8,4), ' | ', mex(8,5), ' | ', mex(8,6), ' | ',
mex(8,7), ' | ', mex(8,8), ' | ', mex(8,9)
write(1,*)'------------------------------------------'
write(1,*)mex(9,1), ' | ', mex(9,2), ' | ', mex(9,3), '
mex(9,4), ' | ', mex(9,5), ' | ', mex(9,6), ' | ',
mex(9,7), ' | ', mex(9,8), ' | ', mex(9,9)

| ',

| ',

| ',

| ',

| ',

| ',

| ',

| ',

| ',

write(1,*)'------------------------------------------'
write(1,*)'
'
write(1,*)'=================================================='
enddo
format(9(2x,i2))
stop
end

C
C RANDOM NUMBER GENERATOR
C
C
-------------------------------------------------------------FUNCTION ran2(idum)
implicit real*8(a-h,o-z)
c
INTEGER idum,IM1,IM2,IMM1,IA1,IA2,IQ1,IQ2,IR1,IR2,NTAB,NDIV
c
real ran2,AM,EPS,RNMX
PARAMETER (IM1=2147483563,IM2=2147483399,AM=1./IM1,IMM1=IM1-1,
*IA1=40014,IA2=40692,IQ1=53668,IQ2=52774,IR1=12211,IR2=3791,
*NTAB=32,NDIV=1+IMM1/NTAB,EPS=1.2e-7,RNMX=1.-EPS)
c
INTEGER idum2,j,k,iv(NTAB),iy
dimension iv(NTAB)
SAVE iv,iy,idum2
DATA idum2/123456789/, iv/NTAB*0/, iy/0/
if (idum.le.0) then
idum=max(-idum,1)
idum2=idum
do 11 j=NTAB+8,1,-1
k=idum/IQ1
idum=IA1*(idum-k*IQ1)-k*IR1
if (idum.lt.0) idum=idum+IM1
if (j.le.NTAB) iv(j)=idum
11
continue
iy=iv(1)
endif
k=idum/IQ1
idum=IA1*(idum-k*IQ1)-k*IR1
if (idum.lt.0) idum=idum+IM1
k=idum2/IQ2
idum2=IA2*(idum2-k*IQ2)-k*IR2
if (idum2.lt.0) idum2=idum2+IM2
j=1+iy/NDIV
iy=iv(j)-idum2
iv(j)=idum
if(iy.lt.1)iy=iy+IMM1
ran2=min(AM*iy,RNMX)
return
END

Vous aimerez peut-être aussi