void expandCube(struct Cube_t *cube) { #if LEVEL > DEBUG_WARN printf("expandCube()\n"); #endif if (cube->depth >= g_uMaxDepth) return; int m; for (m=0;m<UNIQUE_MOVES;m++) { struct Cube_t *childCube = GlobalAlloc(0, sizeof(struct Cube_t)); CopyMemory(childCube, cube, sizeof(struct Cube_t)); DEBUG printf("moveCube() %u\n", m); moveCube(cube, childCube, m); if (!isFirst(childCube, &rootCube)) { DEBUG printf("cube was not first\n"); GlobalFree(childCube); continue; } pruneClones(childCube, &rootCube); childCube->parent = cube; childCube->lastMove = m; childCube->depth = cube->depth + 1; ZeroMemory(&(childCube->nextMove), sizeof(childCube->nextMove)); cube->nextMove[m] = childCube; DEBUG printMoveSequence(childCube); DEBUG putchar('\n'); DEBUG printCube(childCube); if (compareCube(childCube, &solvedCube)) { printMoveSequence(childCube); printCube(childCube); printf("SOLUTION\n"); exit(0); } else { expandCube(childCube); } } }
int main(int argc, char *argv[]) { // check some type sizes debug_size(rootCube.face); debug_size(Cube); debug("\n"); // initialize solved cube Cube_Init(&solvedCube); if (!Cube_LoadTiles(&solvedCube, SOLVED_CUBE_STR, "FURDBL")) { fatal(0, "Failed to load solved cube"); } printf("Target solution:\n"); printCube(&solvedCube); putchar('\n'); // initialize problem cube Cube_Init(&rootCube); rootCube.depth = 0; assert(sizeof(rootCube.face) == sizeof(solvedCube.face)); verify(memcpy(rootCube.face, solvedCube.face, sizeof(rootCube.face)) == rootCube.face); transformCube(&rootCube, MOVE_RACW); transformCube(&rootCube, MOVE_RACW); transformCube(&rootCube, MOVE_LACW); transformCube(&rootCube, MOVE_BCW); transformCube(&rootCube, MOVE_LACW); transformCube(&rootCube, MOVE_BACW); transformCube(&rootCube, MOVE_BCW); transformCube(&rootCube, MOVE_LACW); transformCube(&rootCube, MOVE_BACW); //transformCube(&rootCube, MOVE_LACW); //transformCube(&rootCube, MOVE_BACW); verify(Cube_ValidateTiles(&rootCube)); printf("Cube initial state:\n"); printCube(&rootCube); putchar('\n'); // solve cube Cube *result; for (int gen = 0; gen < 8; gen++) { result = expandGeneration(&rootCube, gen); if (result != NULL) break; printf("Generation %2d: %10u cubes (%10u new)\n", gen, countCubes(&rootCube, -1), countCubes(&rootCube, gen + 1)); } if (result != NULL) { printMoveSequence(result); putchar('\n'); printCube(result); } else { printf("no solution found\n"); } //pause(); return 0; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //initialize rootcube //FILE *fp; //fp = freopen ("rubix.txt", "w", stdout); printf("sizeof(&rootCube->face)=%u\n", sizeof((&rootCube)->face)); ZeroMemory(&rootCube, sizeof(rootCube)); rootCube.parent = NULL; rootCube.lastMove = -1; rootCube.depth = 0; GetFacesString(&rootCube, startCube); printf("starting cube\n"); printCube(&rootCube); //initialize solved cube GetFacesString(&solvedCube, g_szSolvedCubeFaces); printf("target solution\n"); printCube(&solvedCube); struct Cube_t test; CopyMemory(&test, &solvedCube, sizeof(struct Cube_t)); transformCube(&test, MOVE_RACW); transformCube(&test, MOVE_RACW); transformCube(&test, MOVE_LACW); transformCube(&test, MOVE_BCW); transformCube(&test, MOVE_LACW); transformCube(&test, MOVE_BACW); transformCube(&test, MOVE_BCW); transformCube(&test, MOVE_LACW); transformCube(&test, MOVE_BACW); transformCube(&test, MOVE_LACW); //transformCube(&test, MOVE_BACW); printf("test cube\n"); printCube(&test); CopyMemory(&rootCube, &test, sizeof(struct Cube_t)); int g; struct Cube_t *result; LARGE_INTEGER liStart, liFinish, liFrequency; QueryPerformanceFrequency(&liFrequency); for (g=0;g<g_uMaxDepth;g++) { QueryPerformanceCounter(&liStart); result = expandGeneration(&rootCube, g); if (result != NULL) break; QueryPerformanceCounter(&liFinish); printf("Cubes at generation %3u: %10u (new: %10u); time taken: %8.6f s\n", g, countCubes(&rootCube, -1), countCubes(&rootCube, g+1), (double)(liFinish.QuadPart-liStart.QuadPart)/(double)liFrequency.QuadPart); } if (result != NULL) { printMoveSequence(result); putchar('\n'); printCube(result); } else { printf("no solution found\n"); } pause(); return 0; }
void printParticle(particle *particles, float isovalue) { int p, i, j, k; isoParticle(particles, isovalue); glBegin( GL_POINTS ); p = 0; //for(p = 0; p < NUM_PARTICLES; p++) //{ glColor3f(particles[p].r, particles[p].g, particles[p].b); glVertex3f(particles[p].x, particles[p].y, particles[p].z); //glColor3f( 0.5f, 0.5f, 0.5f ); for(i = 0; i < DIV_X * DIV_Y * DIV_Z; i++) { if(particles[p].vertexs[i].sel) glColor3f( 1.0f, 0.0f, 1.0f ); else glColor3f( 0.0f, 1.0f, 1.0f ); glVertex3f( particles[p].vertexs[i].x, particles[p].vertexs[i].y, particles[p].vertexs[i].z); } //} for (i = 0; i < NUM_CUBES; i++) { printCube(particles[p].cubes[i], particles[p].vertexs[i].sel); } glEnd(); }
void Rubik::optimise() { int size = this->moves.size(); if (size < 2) { return; } if (Rubik::debug) { std::cout << "Optimising" << std::endl; printCube(); std::cout << std::endl; for (unsigned int i = 0; i < this->moves.size(); i++) { std::cout << moves[i] << " "; } this->printCube(); std::cout << "--------------------------------------------------------" << std::endl; } int index = 0; while (index < size - 1) { index = index >= 0 ? index : 0; index = index + 1 - shortenMoves(index); size = this->moves.size(); } }
void generateCube(Cube *parentCube, enum Move_e thisMove) { Cube *thisCube; #ifndef NDEBUG printMoveSequence(parentCube); debug(", %d\n", thisMove); #endif if (thisMove == -1) { debug("cube is root\n"); thisCube = parentCube; goto justmove; } //alloc space for new cube //thisCube = GlobalAlloc(0, sizeof(Cube)); thisCube = malloc(sizeof(Cube)); //generate new cube //CopyMemory(thisCube, parentCube, sizeof(Cube)); memcpy(thisCube, parentCube, sizeof(Cube)); thisCube->depth = parentCube->depth + 1; moveCube(parentCube, thisCube, thisMove); //check cube validity debug("validating cube\n"); if (!validateCube(&rootCube, thisCube)) { //GlobalFree(thisCube); free(thisCube); return; } //take action and set handles //ZeroMemory(&thisCube->nextMove, sizeof(thisCube->nextMove)); memset(&thisCube->nextMove, '\0', sizeof(thisCube->nextMove)); thisCube->parent = parentCube; thisCube->lastMove = thisMove; parentCube->nextMove[thisMove] = thisCube; //make additional moves if appropriate justmove: debug("checking if cube is solution\n"); if (compareCube(thisCube, &solvedCube)) { printf("cube matched solution\n"); printMoveSequence(thisCube); printf("\n"); printCube(thisCube); pause(); return; } //if (thisCube->depth >= g_uMaxDepth) return; int m; for (m=0; m<UNIQUE_MOVES; m++) { generateCube(thisCube, m); } return; }
void expandCube(Cube *cube) { //if (cube->depth >= g_uMaxDepth) return; int m; for (m=0; m<UNIQUE_MOVES; m++) { //Cube *childCube = GlobalAlloc(0, sizeof(Cube)); Cube *childCube = malloc(sizeof(Cube)); //CopyMemory(childCube, cube, sizeof(Cube)); memcpy(childCube, cube, sizeof(Cube)); debug("moveCube() %u\n", m); moveCube(cube, childCube, m); if (!isFirst(childCube, &rootCube)) { debug("cube was not first\n"); //GlobalFree(childCube); free(childCube); continue; } pruneClones(childCube, &rootCube); childCube->parent = cube; childCube->lastMove = m; childCube->depth = cube->depth + 1; //ZeroMemory(&(childCube->nextMove), sizeof(childCube->nextMove)); memset(&childCube->nextMove, '\0', sizeof(childCube->nextMove)); cube->nextMove[m] = childCube; #ifndef NDEBUG printMoveSequence(childCube); putchar('\n'); printCube(childCube); #endif if (compareCube(childCube, &solvedCube)) { printMoveSequence(childCube); printCube(childCube); printf("SOLUTION\n"); exit(0); } else { expandCube(childCube); } } }
void generateCube(struct Cube_t *parentCube, enum Move_e thisMove) { struct Cube_t *thisCube; DEBUG printMoveSequence(parentCube); DEBUG printf(", %d\n", thisMove); if (thisMove == -1) { DEBUG printf("cube is root\n"); thisCube = parentCube; goto justmove; } //alloc space for new cube thisCube = GlobalAlloc(0, sizeof(struct Cube_t)); //generate new cube CopyMemory(thisCube, parentCube, sizeof(struct Cube_t)); thisCube->depth = parentCube->depth + 1; moveCube(parentCube, thisCube, thisMove); //check cube validity DEBUG printf("validating cube\n"); if (!validateCube(&rootCube, thisCube)) { GlobalFree(thisCube); return; } //take action and set handles ZeroMemory(&thisCube->nextMove, sizeof(thisCube->nextMove)); thisCube->parent = parentCube; thisCube->lastMove = thisMove; parentCube->nextMove[thisMove] = thisCube; //make additional moves if appropriate justmove: DEBUG printf("checking if cube is solution\n"); if (compareCube(thisCube, &solvedCube)) { printf("cube matched solution\n"); printMoveSequence(thisCube); printf("\n"); printCube(thisCube); pause(); return; } if (thisCube->depth >= g_uMaxDepth) return; int m; for (m=0;m<UNIQUE_MOVES;m++) { generateCube(thisCube, m); } return; }
int main(int argc, char **argv) { long Target = 75; long moving_target = Target; int i,rc; const int Nthreads = 32; pthread_t thread_id[Nthreads]; GSList *gl[Nthreads]; char fname[64]; FILE *fout; int zeros[13]= {0,0,0,0,0,0,0,0,0,0,0,0,0}; if(argc==2) { Target = strtol(argv[1],NULL,10); } printf("Target value is %ld\n",Target); for(i=0; i<Nthreads; i++) { rc = pthread_create( &thread_id[i], NULL, searchScubes, (void*)moving_target); if(rc != 0) printf("Error Thread create %d: rc = %d\n",i,rc); moving_target += 2; } for(i=0; i<Nthreads; i++) { rc = pthread_join( thread_id[i], (void**)&gl[i]); if(rc != 0) printf("Error Thread join %d: rc = %d\n",i,rc); moving_target = Target+(i*2); if(gl[i] != NULL) { // code to write each set of signatures to a unique file. // filename Scube_%03d.int13 // for each signature write an array of 13 integers to disk // incorporate the code into the pthread_join section sprintf(fname,"./results/multi/Scube_%03d.int13",CPTR(gl[i])->Signature[12]); fout = fopen(fname,"w"); if(fout != NULL) { fwrite(CPTR(gl[i])->Signature, sizeof(int), 13, fout); fclose(fout); } } else { // No cubes found - write signature of 12 zeros and target zeros[12]=Target+(i*2); sprintf(fname,"./results/zero/Scube_%03ld.int13",Target+(i*2)); fout = fopen(fname,"w"); fwrite(zeros, sizeof(int), 13, fout); fclose(fout); } } #if(0) // This section will print any cubes found. GList *cubes; for(i=0; i<Nthreads; i++) { cubes = gl[i]; while(cubes != NULL) { if(CPTR(cubes)->duplicate == 0) { int x; printf("\tScube signature:\n"); for(x=0; x<12; x++) printf("%02d ",CPTR(cubes)->Signature[x]); printf("\nTarget: %02d\n",CPTR(cubes)->Signature[12]); printCube(CPTR(cubes)); printf("\n"); } cubes = g_list_next(cubes); } } #endif // Cleanup pthread_exit(NULL); }