-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknight.c
46 lines (40 loc) · 938 Bytes
/
knight.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// marciosdn
// v.2 - 2020-01-14 13:50
#include <stdio.h>
#include <string.h>
#define SIZEx 8
#define SIZEy 8
int Sx[] = { 2, 1,-1,-2,-2,-1, 1, 2};
int Sy[] = {-1,-2,-2,-1, 1, 2, 2, 1};
int T[SIZEx][SIZEy]; //Board x by y, zero filled.
int step = 1;
int sol = 1; //Solutions.
void jump(int step, int x, int y) {
if (!T[x][y]) { //If 0, it can walk.
T[x][y] = step++;
if (step>SIZEx*SIZEy) { //Is it finished, last step?
printf("\nSolution: %d\n", sol++);
printT(); //print it!
} else {
for(int k=0;k<8;k++) //Try 8 directions.
if (x+Sx[k]>-1 && y+Sy[k]>-1 && x+Sx[k]<SIZEx && y+Sy[k]<SIZEy)
jump(step,x+Sx[k],y+Sy[k]); //go!
}
step--;
T[x][y] = 0;
}
}
void printT(void) {
for(int i=0;i<SIZEx;i++) {
for(int j=0;j<SIZEy;j++)
printf(" %2d",T[i][j]);
printf("\n");
}
}
void main(void) {
for(int i=0;i<SIZEx;i++) {
for(int j=0;j<SIZEy;j++) {
jump(step,i,j);
}
}
}