From 7f7cf6a03254f0ab0ec58572adda1fc92b2506a6 Mon Sep 17 00:00:00 2001 From: Quentin Carbonneaux Date: Mon, 12 Oct 2015 21:46:27 -0400 Subject: add for loops --- minic/test/queen.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'minic/test') diff --git a/minic/test/queen.c b/minic/test/queen.c index c998e2a..86058d2 100644 --- a/minic/test/queen.c +++ b/minic/test/queen.c @@ -8,18 +8,13 @@ print() { int x; int y; - y = 0; - while (y < 8) { - x = 0; - while (x < 8) { + for (y=0; y<8; y++) { + for (x=0; x<8; x++) if (t[x + 8*y]) printf(" Q"); else printf(" ."); - x++; - } printf("\n"); - y++; } printf("\n"); } @@ -29,8 +24,7 @@ chk(int x, int y) { int r; r = 0; - i = 0; - while (i < 8) { + for (i=0; i<8; i++) { r = r + t[x + 8*i]; r = r + t[i + 8*y]; if (x+i < 8 & y+i < 8) @@ -41,11 +35,8 @@ chk(int x, int y) { r = r + t[x-i + 8*(y+i)]; if (x-i >= 0 & y-i >= 0) r = r + t[x-i + 8*(y-i)]; - if (r) - return 1; - i++; } - return 0; + return r; } go(int n, int x, int y) { @@ -54,17 +45,14 @@ go(int n, int x, int y) { N++; return 0; } - while (y < 8) { - while (x < 8) { + 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++; - } x = 0; - y++; } } -- cgit 1.4.1