summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--minic/test/queen.c65
1 files changed, 36 insertions, 29 deletions
diff --git a/minic/test/queen.c b/minic/test/queen.c
index 40c2635..e199189 100644
--- a/minic/test/queen.c
+++ b/minic/test/queen.c
@@ -1,16 +1,17 @@
 int printf();
-int *calloc();
+void *calloc();
 
+int Q;
 int N;
-int *t;
+int **t;
 
 print() {
 	int x;
 	int y;
 
-	for (y=0; y<8; y++) {
-		for (x=0; x<8; x++)
-			if (t[x + 8*y])
+	for (y=0; y<Q; y++) {
+		for (x=0; x<Q; x++)
+			if (t[x][y])
 				printf(" Q");
 			else
 				printf(" .");
@@ -23,40 +24,46 @@ chk(int x, int y) {
 	int i;
 	int r;
 
-	for (r=i=0; i<8; i++) {
-		r = r + t[x + 8*i];
-		r = r + t[i + 8*y];
-		if (x+i < 8 & y+i < 8)
-			r = r + t[x+i + 8*(y+i)];
-		if (x+i < 8 & y-i >= 0)
-			r = r + t[x+i + 8*(y-i)];
-		if (x-i >= 0 & y+i < 8)
-			r = r + t[x-i + 8*(y+i)];
+	for (r=i=0; i<Q; i++) {
+		r = r + t[x][i];
+		r = r + t[i][y];
+		if (x+i < Q & y+i < Q)
+			r = r + t[x+i][y+i];
+		if (x+i < Q & y-i >= 0)
+			r = r + t[x+i][y-i];
+		if (x-i >= 0 & y+i < Q)
+			r = r + t[x-i][y+i];
 		if (x-i >= 0 & y-i >= 0)
-			r = r + t[x-i + 8*(y-i)];
+			r = r + t[x-i][y-i];
 	}
 	return r;
 }
 
-go(int n, int x, int y) {
-	if (n == 8) {
+go(int y) {
+	int x;
+
+	if (y == Q) {
 		print();
 		N++;
 		return 0;
 	}
-	for (; y<8; y++) {
-		for (; x<8; x++)
-			if (chk(x, y) == 0) {
-				t[x + 8*y]++;
-				go(n+1, x, y);
-				t[x + 8*y]--;
-			}
-		x = 0;
-	}
+	for (x=0; x<Q; x++)
+		if (chk(x, y) == 0) {
+			t[x][y]++;
+			go(y+1);
+			t[x][y]--;
+		}
 }
 
-main() {
-	t = calloc(64, sizeof(int));
-	go(0, 0, 0);
+main(int ac, void **av) {
+	int i;
+
+	Q = 8;
+	if (ac >= 2)
+		Q = atoi(av[1]);
+	t = calloc(Q, sizeof(int *));
+	for (i=0; i<Q; i++)
+		t[i] = calloc(Q, sizeof(int));
+	go(0);
 	printf("found %d solutions\n", N);
 }