qbe

Internal scc patchset buffer for QBE
Log | Files | Refs | README | LICENSE

knight.c (776B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 
      4 void *calloc();
      5 
      6 int N;
      7 int **b;
      8 
      9 board()
     10 {
     11 	int x;
     12 	int y;
     13 
     14 	for (y=0; y<8; y++) {
     15 		for (x=0; x<8; x++)
     16 			printf(" %02d", b[x][y]);
     17 		printf("\n");
     18 	}
     19 	printf("\n");
     20 	return 0;
     21 }
     22 
     23 chk(int x, int y)
     24 {
     25 	if (x < 0 || x > 7 || y < 0 || y > 7)
     26 		return 0;
     27 	return b[x][y] == 0;
     28 }
     29 
     30 go(int k, int x, int y)
     31 {
     32 	int i;
     33 	int j;
     34 
     35 	b[x][y] = k;
     36 	if (k == 64) {
     37 		if (x != 2 && y != 0 && abs(x-2) + abs(y) == 3) {
     38 			board();
     39 			N++;
     40 			if (N == 10)
     41 				exit(0);
     42 		}
     43 	} else
     44 		for (i=-2; i<=2; i++)
     45 			for (j=-2; j<=2; j++) 
     46 				if (abs(i) + abs(j) == 3 && chk(x+i, y+j)) 
     47 					go(k+1, x+i, y+j);
     48 	b[x][y] = 0;
     49 	return 0;
     50 }
     51 
     52 main()
     53 {
     54 	int i;
     55 
     56 	b = calloc(8, sizeof (int *));
     57 	for (i=0; i<8; i++)
     58 		b[i] = calloc(8, sizeof (int));
     59 	go(1, 2, 0);
     60 }