You are on page 1of 1

Backtracking calul

#include<stdio.h>
#include<conio.h>
int a[8]={2,1,-1,-2,-2,-1,1,2}, b[8]={1,2,2,1,-1,-2,-2,-1};
int t[10][10],m,n;
void tipareste(void)
{
int p,r;
for(p=0;p<m;p++)
{ for(r=0; r<n; r++) printf("%3d",t[p][r]);
putchar('\n'); }
getch();
}
void cal(int i, int j, int k)
{
int i1,j1,d;
for(d=0;d<8;d++) { i1=i+a[d];
j1=j+b[d];
if((i1<m)&&(j1<n)&&(i1>=0)&&(j1>=0)&&(!t[i1][j1]))
{ t[i1][j1]=k;
if(k<m*n) cal(i1,j1,k+1);
else { tipareste();
putchar('\n');
t[i1][j1]=0; }
}
}
t[i][j]=0;
}
void main(void)
{
int i,j;
clrscr();
printf("introdu nr de lin: ");
scanf("%d",&m);
printf("introdu nr de col: ");
scanf("%d",&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++) t[i][j]=0;
t[0][0]=1;
cal(0,0,2);
}

You might also like