X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Ftest%2Fqueens-handoptimized.c;h=0f71a4a98a2401139494a92494a615a271f94c48;hb=1872920c09708b361d06c0dc9f4c1fd0a03544f5;hp=b9bf0c1476204d9c40f0b154dcde4076327a3e29;hpb=ce9964a1630d51399cc0fab55b598743ec522419;p=libfirm diff --git a/ir/be/test/queens-handoptimized.c b/ir/be/test/queens-handoptimized.c index b9bf0c147..0f71a4a98 100644 --- a/ir/be/test/queens-handoptimized.c +++ b/ir/be/test/queens-handoptimized.c @@ -26,7 +26,7 @@ typedef int boolean; //static int *row; // queen in column c is at row[c] -static inline int myabs(int i) { +static int myabs(int i) { if(0 > i) i = -i; return(i); @@ -38,8 +38,25 @@ static inline boolean place_ok (int i, const int *r, int ri) { int j = 0; boolean res; +#if 0 + if(0 >= i) + return true; + + do { + int rj = r[j]; + if ((rj == ri) || ((myabs(ri-rj)) == (i-j))) { + res = false; + return(res); + } + j = j+1; + } while(j < i); + + res = true; + return(res); +#else while (j < i) { - if ((r[j] == ri) || ((myabs(ri-r[j])) == (i-j))) { + int rj = r[j]; + if ((rj == ri) || ((myabs(ri-rj)) == (i-j))) { res = false; return(res); } @@ -48,38 +65,38 @@ static inline boolean place_ok (int i, const int *r, int ri) { res = true; return(res); +#endif } int solve (int n) { // return the number of solutions to the n-queens problem int c = 0; int res = 0; - int *row; + int *row; row = malloc(sizeof(*row) * n); row[0] = -1; while (c >= 0) { - int ri; -#if 1 + int rc = row[c]; + do { - row[c] = ri = row[c]+1; - } while ((ri < n) && (!place_ok(c, row, ri))); -#else - row[c] = row[c]+1; - while ((row[c] < n) && (!place_ok(c, row))) { - row[c] = row[c]+1; - } -#endif - if (row[c] < n) { // successfully placed at (c,row[c]) + rc++; + } while ((rc < n) && (!place_ok(c, row, rc))); + + if (rc < n) { // successfully placed at (c,row[c]) + row[c] = rc; + if (c == n-1) res = res+1; else { c = c+1; row[c] = -1; } + continue; } - else // dead end, track back - c = c-1; + + row[c] = rc; + c = c-1; } free(row);