dimanche 4 avril 2010

chess knight problem


Hello amateurs of elementary math, have you ever heard about chess knight problem?

Here it is:
One should find route of a knight starting from a fixed chess board (8x8) cell, passing every cell once and only once and
at the end, passed by every cell of the chess board.

I put in my blog the simplest solution. If we have in mind a cyclical route, that is, from 64th step the knight goes on 1st,
the solution for any cell of the board is given by simple going by circle.
Look, this is a fortran code realising this algorithm (23 lines):

program chess
integer X(8,8),Y(8,8)
integer k,l
data X/40,7,32,51,56,49,30,45,33,52,41,48,31,46,57,60,8,39,6,55,50
c,59,44,29,53,34,63,42,47,4,61,58,38,9,54,5,62,43,28,19,35,24,37,64
c,3,18,13,16,10,1,22,25,12,15,20,27,23,36,11,2,21,26,17,14/

1 write(*,*)'Enter origin knight coordinates (column row):'
read(*,*)k,l
if (k.ge.1.and.k.le.8.and.l.ge.1.and.l.le.8) goto 2
write(*,*)'Input error. Repeat last enter'
goto 1

2 continue
do 3,i=1,8
do 3,j=1,8
if (X(j,i).lt.X(k,l)) then
Y(j,i)=65+X(j,i)-X(k,l)
else
Y(j,i)=X(j,i)+1-X(k,l)
endif
3 continue
write(*,4) ((Y(j,i),j=1,8),i=1,8)
4 format(//,8(2X,I2))
end

Aucun commentaire:

Enregistrer un commentaire