#include #include #include #define NTICK 10 #define BARSIZE 50 #define MAXSTEP (BARSIZE - NTICK) const char *restart = "restart.txt"; const char *restart2 = "restart.tmp"; static int Step[NTICK]; int init(const char *fnam) { int i; if (!fnam) { for (i = 0; i < NTICK; i++) { Step[i] = 1; } } else { FILE *ifp; ifp = fopen(fnam, "r"); for (i = 0; i < NTICK; i++) { fscanf(ifp, "%i", &(Step[i])); } fclose(ifp); } return 0; } int save(void) { FILE *fp; int i; fp = fopen(restart2, "w"); if (fp == NULL) { goto err; } for (i = 0; i < NTICK; i++) { if (fprintf(fp, "%d ", Step[i]) < 0) { goto err; } } if (fprintf(fp, "\n") < 0) { goto err; } if (fclose(fp) < 0) { goto err; } if (rename(restart2, restart) < 0) { goto err; } usleep(10000); return 0; err: perror("save"); return -1; } int next(void) { int i = 0; while (1) { #if 0 {int j; for (j=0;j= 8) { printf("carry %d\n", i); save(); } return 0; } } Step[i] = 1; i++; if (i >= NTICK) { printf("exhausted all possibility\n"); return -1; } } } void make_pos(int pos[NTICK + 2]) { int i; pos[0] = 0; for (i = 0; i < NTICK; i++) { pos[i + 1] = pos[i] + Step[i]; } pos[NTICK + 1] = BARSIZE; } int fill_combi(int pos[NTICK + 2], int combi[BARSIZE + 1]) { int i, j, r; for (i = 0; i < BARSIZE; i++) { combi[i] = 0; } for (i = 0; i < (NTICK + 2); i++) { for (j = 0; j < (NTICK + 2); j++) { int dif = pos[i] - pos[j]; if (dif < 0) dif = -dif; combi[dif] = 1; } } r = 0; for (i = 1; i < BARSIZE; i++) { if (!combi[i]) { r++; } } return r; } int pass(void) { int pos[NTICK + 2]; int combi[BARSIZE + 1]; int c; make_pos(pos); c = fill_combi(pos, combi); if (c < 3) { int i; printf("%02d < [ ", c); for (i = 0; i < (NTICK + 2); i++) { printf("%02d ", pos[i]); } printf("]\n"); if (c) { printf("no_good = [ "); for (i = 1; i < BARSIZE; i++) { if (!combi[i]) { printf("%02d ", i); } } printf("]\n"); } fflush(stdout); } return 0; } int main(int argc, char **argv) { int r = 2; int i; init(NULL); for (i = 0; argv[i]; i++) { if (argv[i][0] == '-') { } else { init(argv[i]); } } while (1) { if (pass()) { r = 0; } if (next() != 0) { return r; } } }