Ayuda recorrido del caballo en C

Rocio
05 de Febrero del 2006
Hola! Necesitaria, si alquien lo tiene, el recorrido del caballo con backtracking. Mil gracias!!!!

Noel Solw
05 de Febrero del 2006
// program k7c4 - page 136
// recursive knight\'s tour.
// 7/2/2002
// written in Borland CPP ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <dos.h>

#define N 8
#define MAX N*N
#define dx 5
#define dy 2
#define xo (80 - N*dx)/2
#define yo (25 - N*dy)/2

unsigned char a[N][N];

struct delta
{
char di;
char dj;
} d[8] = {{ 1,-2},
{ 2,-1},
{ 2, 1},
{ 1, 2},
{-1, 2},
{-2, 1},
{-2,-1},
{-1,-2}};

void Init()
{
for(char i = 0;i < N;i++)
for(char j = 0;j < N;j++)
a[i][j] = 0;
} // INIT

void Board()
{
char x;
for(char y = yo;y <= yo + N*dy;y += dy)
{
x = xo;
gotoxy(x,y);
for(;x < xo+N*dx;)
{
cout << "+";
x++;
for(char i = 0;i < 4;i++)
{
cout << "-";
x++;
}
}
cout << "+";
}
for(y = yo+1;y < yo + N*dy;y += dy)
for(x = xo;x <= xo+N*dx;x+=dx)
{
gotoxy(x,y);
cout << "|";
}
} // BOARD

void ShowStep(char i,char j,int step = 0)
{
gotoxy(xo+j*dx+2,yo+i*dy+1);
cout << setw(2);
if(step)
cout << step;
else
cout << "*";
delay(300);
} // SHOW STEP

void ShowCounter()
{
static unsigned long counter = 0;
counter++;
if(counter % 10000 == 0)
{
gotoxy(65,1);
cout << setw(15) << counter;
}
} // SHOW COUNTER

char Process(char i,char j,char step)
{
ShowCounter();
if(step > MAX)
return 1; // success, search end
if(i < 0 || j < 0 || i >= N || j >= N)
return 0; // out of board
if(a[i][j])
return 0;
a[i][j]++;
for(char k = 0;k < 8;k++)
if(Process(i+d[k].di,j+d[k].dj,step+1))
{
ShowStep(i,j,step);
return 1; // success
}
a[i][j] = 0;
return 0; // fail
} // PROCESS

void main()
{
clrscr();
Init();
gotoxy(xo+6,2);
cout << "recursive knight\'s tour";
_setcursortype(_NOCURSOR);
Board();
gotoxy(xo+5,23);
cout << "wait 30 seconds, please ! ! !\n";
char i = 0, j = 0;
ShowStep(i,j);
Process(i,j,1);
gotoxy(xo+5,23);
cout << "end of program - good bye ! ! !\n";
getch();
_setcursortype(_NORMALCURSOR);
} // MAIN