From eee9afb88edd7094adcabc6d111526de983ac5ee Mon Sep 17 00:00:00 2001 From: Quentin Carbonneaux Date: Tue, 13 Oct 2015 14:30:53 -0400 Subject: use an array of pointers in queen --- minic/test/queen.c | 65 ++++++++++++++++++++++++++++++------------------------ 1 file 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= 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= 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= 2) + Q = atoi(av[1]); + t = calloc(Q, sizeof(int *)); + for (i=0; i