crooks

combinatorial rooks
git clone https://a3nm.net/git/crooks/
Log | Files | Refs

commit 1d9b390f1c02faeed11e3937674e8fb6a053e45c
parent b5fb0538080d7eea690fd5775ba206bf9f844c16
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat,  7 Jun 2014 19:34:23 +0200

easy optims

Diffstat:
main.c | 22++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/main.c b/main.c @@ -132,6 +132,8 @@ int upd_vio(int z, int x, int y, int v, int d) { new = A[z][x][y] | (1 << d); else new = A[z][x][y] & ~(1 << d); + if (old == new) + return 0; // nothing changed for (int p = 0; p < N; p++) { if (p != z) { val = ((new | (1 << 0)) == 7 ? 1 : 0) - ((old | (1 << 0)) == 7 ? 1 : 0); @@ -336,8 +338,9 @@ int random_move() { int z = R[rook][0]; int x = R[rook][1]; int y = R[rook][2]; + int delta = 0; G[z][x][y] = 0; - upd_atk_rm(rook); + delta += upd_atk_rm(rook); int nx, ny, nz; while (1) { nx = (rand() % N); @@ -352,10 +355,8 @@ int random_move() { R[rook][0] = nz; R[rook][1] = nx; R[rook][2] = ny; - // recompute everything - // TODO: don't do this - upd_atk_add(rook); - return num_violations(); + delta += upd_atk_add(rook); + return delta; } int position(int i) { @@ -369,15 +370,11 @@ int position(int i) { for (int z = 0; z < N; z++) for (int x = 0; x < N; x++) for (int y = 0; y < N; y++) - if (!A[z][x][y] && !G[z][x][y]) + if (!A[z][x][y] && !G[z][x][y]) { if (V[z][x][y] < best) { best = V[z][x][y]; + nbs = 0; } - // best is now the minimal number of violations - for (int z = 0; z < N; z++) - for (int x = 0; x < N; x++) - for (int y = 0; y < N; y++) - if (!A[z][x][y] && !G[z][x][y]) if (V[z][x][y] == best) { bs[nbs][0] = z; bs[nbs][1] = x; @@ -385,6 +382,7 @@ int position(int i) { nbs++; if (debug) printf("%d %d %d is a fitting position\n", z, x, y); } + } // choose random cell among optimal ones int npos = rand() % nbs; int bz = bs[npos][0]; @@ -505,7 +503,7 @@ int main(int argc, char **argv) { if (useless > UBOUND) { // too many useless steps, do a random move useless = 0; - tot = random_move(); + tot += random_move(); } } // we have found a configuration for K rooks; display it