#define SIZE_EDGE (3) #define SIZE_ROT (2) #define PROGRESS 5000 #define ROT_ONEKEY 1 /* #define DEBUG 1 */ #define NDEBUG 1 #include #include #include #include #include #include #define SIZE_TOT (SIZE_EDGE*SIZE_EDGE) typedef unsigned char num; typedef unsigned int pos; typedef num field[SIZE_TOT]; #define INVALID_POS ((pos)-1) #define RC_ENTRY(x,y) ((y)*SIZE_EDGE+(x)) #if SIZE_EDGE==3 #define STATES_TOT (362880) #define FILENAME_23 "Nokia61_23.cache" #define ROUNDCYCLE_TOT (sizeof(roundcycle)/sizeof(*roundcycle)) static const unsigned char roundcycle[]={ RC_ENTRY(0,0), RC_ENTRY(1,0), RC_ENTRY(1,1), RC_ENTRY(0,1), }; #else #error "STATES_TOT not known!" #endif struct state { pos todo_next; pos parent; pos depth; } states[STATES_TOT]; pos todo=INVALID_POS; pos *todo_tail=&todo; #ifdef PROGRESS pos states_touched=0,states_retouched=0,states_done=0; pos maxdepth=0; #endif static const num ascending[25]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; static int check_field(const field fld) { unsigned char used[SIZE_TOT]; int i; memset(used,0,sizeof(used)); for (i=0;i=SIZE_TOT || used[fld[i]]) return(0); used[fld[i]]=1; } return(1); } #define assert_field(fld) assert(check_field(fld)) static void dump_field(const field fld) { unsigned char y,x; for (y=0;y=0 && pparent==INVALID_POS || state->depth>depth)) return; #ifdef PROGRESS if (maxdepthparent=parent; state->depth=depth; if (state->todo_next==INVALID_POS) { *todo_tail=p; todo_tail=&state->todo_next; #ifdef PROGRESS states_touched++; #endif } else { #ifdef PROGRESS states_retouched++; #endif } } static void progress_dump(void) { #ifdef PROGRESS printf("TOUCHED=%6u (RETOUCHED %6u), DONE=%6u, KNOWN TODO=%6u, UNTOUCHED=%6u, MAXDEPTH=%6u\n", states_touched,states_retouched,states_done,states_touched-states_done,STATES_TOT-states_touched,maxdepth); #endif } #ifdef PROGRESS static void states_progress(void) { static unsigned period=0; static time_t time_last=0; time_t time_new; if (period++depth+1); } todo=state->todo_next; state->todo_next=INVALID_POS; #ifdef PROGRESS states_done++; states_progress(); #endif } } static void final(void) { #ifdef PROGRESS pos *maxes,p; int maxi; #endif puts("FINAL:"); progress_dump(); #ifdef PROGRESS fputs("Depth distribution:",stdout); maxes=malloc(sizeof(*maxes)*(maxdepth+1)); memset(maxes,0,sizeof(maxes)); for (p=0;p>16); states_23[p].a[1]=(states[p].parent>> 8); states_23[p].a[2]=(states[p].parent>> 0); } if (fwrite(states_23,1,STATES_23_SIZEOF,f)!=STATES_23_SIZEOF) { fail_free: free(states_23); goto fail_unlink; } if (fclose(f)) goto fail_free; free(states_23); #ifdef PROGRESS printf("Cache written to \"%s\".\n",FILENAME_23); #endif } #endif /* SIZE_EDGE==3 */ static void input(void) { field fld; int x,y,i; pos p; struct state *state; for (;;) { restart: for (y=0;y"); p=field_to_pos(fld); do { state=states+p; printf("STATE %6u: DEPTH=%6u, PARENT=%6u\n",p,state->depth,state->parent); pos_to_field(fld,p); dump_field(fld); puts("----------"); p=state->parent; } while (state!=states+0); putchar('\n'); } } int main(void) { #if SIZE_EDGE==3 if (read_23()) #endif { memset(states,-1,sizeof(states)); mark(0,0,0); cycle(); final(); #if SIZE_EDGE==3 write_23(); #endif } input(); return(EXIT_SUCCESS); }