int main() { int K, ox, oy; while (scanf("%d", &K) == 1) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { scanf("%d", &g[i][j]); if (g[i][j] == 0) ox = i, oy = j; } } if (!solvable(g)) { puts("0"); } else { int baseH; solved = 0, ida_depth = baseH = Hinit(g); if (ida_depth == 0) { puts("1"); // puts("0"); } else { while (solved == 0 && ida_depth <= K) ida_depth = IDA(0, ox, oy, baseH, -1); if (solved && solved <= K) puts("1"); else puts("0"); // printf("%d\n", solved); } } } return 0; }
/* * * name: main * * Recieves information from the user of the input and output files, and then * makes appropriate calls. * * @param argc the number of arguments passed (including the program) * @param argv the argument array of the program call * @return error code */ int main(int argc, char ** argv){ // The user can pass a parameter to the program for the file name. // If no parameter is given, the program will ask explicitly. char input[MAX_FILE_LEN]; if(argc == 2){ strcpy(input, argv[1]); } else{ printf("\n Name of your input file (%d characters max): ", MAX_FILE_LEN); scanf("%s", input); } struct stateNode init; readState(&init, input); struct stateNode goal = { 0, // heuristic {GOAL_STATE}, // state 0, // pathCost NULL // predecessor }; h(&init, &goal); if(!solvable(&init, &goal)){ printNode(&init); printf("\nUnsolvable\n\n"); return 0; } printNodePath(search(&init, &goal)); return 0; }
//POJ 3648 int main() { int n, m; char s1[10], s2[10]; while (scanf("%d%d", &n, &m) == 2) { if (n == 0 && m == 0) { break; } init(); while (m--) { scanf("%s%s", s1, s2); int u = change(s1); int v = change(s2); addedge(u ^ 1, v); addedge(v ^ 1, u); } addedge(1, 0); if (solvable(2 * n)) { solve(2 * n); for (int i = 1; i < n; i++) { //注意这一定是判断color[Belong[ if (color[Belong[2 * i]] == 'R') { printf("%dw", i); } else { printf("%dh", i); } if (i < n - 1) { printf(" "); } else { printf("\n"); } } } else { printf("bad luck\n"); } } }
void Board::setSolvableFlag(bool value) { if(value && !_solvable_flag && !solvable()){ _solvable_flag = value; newGame(); } else _solvable_flag = value; }
void SolveCube2::MakeRandomCube(Cube *cube) { //step 1 MakeRandomCubeCore(); PrintState(); assert(solvable()); //step2 assert(cube->n()==2); fill_corner_pointer(cube); GetColorData(); corner2cube(); }
void MainWindow::openmix() { do{ for(int i=0;i<5;i++) { for(int j=0;j<5;j++) grid[i][j]=(rand()%2); } }while(!solvable(grid,n1,n2)); for(int i=0;i<5;i++) { for(int j=0;j<5;j++) if (grid[i][j]==1) {b[i][j]->setStyleSheet("QPushButton {border-image: url(C:/Users/Altair/Documents/Qt/Inversion/on.png) ;}");}else {b[i][j]->setStyleSheet("QPushButton { border-image: url(C:/Users/Altair/Documents/Qt/Inversion/off.png) ; }");} } }
void SolveCube2::GetData(Cube *cube)//kind of color less then 32 { assert(cube->n()==2); fill_corner_pointer(cube); GetColorData(); for(int i=0;i<8;i++) { corner_permutation_[i]=get_corner_id_from_cube(*corner_pointer_[i][0],*corner_pointer_[i][1],*corner_pointer_[i][2]); } for(int i=0;i<8;i++) { corner_twist_[i]=get_corner_twist_from_cube(*corner_pointer_[i][0],*corner_pointer_[i][1],*corner_pointer_[i][2]); } PrintState(); assert(solvable()); }
void MainWindow::openedit() { if (resett->isEnabled()==true) { resett->setEnabled(false); mix->setEnabled(false); solve->setEnabled(false); combo->setEnabled(false); edit->setStyleSheet("QPushButton{border-image: url(C:/Users/Altair/Documents/Qt/Inversion/bouton-clicked.png)3 10 3 10;color: grey;border-top: 3px transparent; border-bottom: 3px transparent;border-right: 10px transparent;border-left: 10px transparent;}"); for (int i=0; i < 5; i++) { for (int j=0; j < 5; j++) { b[i][j]->setCheckable(true); connect(b[i][j], SIGNAL(clicked()), this, SLOT(clik1())); } } } else { resett->setEnabled(true); mix->setEnabled(true); solve->setEnabled(true); combo->setEnabled(true); edit->setStyleSheet("QPushButton{border-image: url(C:/Users/Altair/Documents/Qt/Inversion/bouton.png)3 10 3 10;color: grey;border-top: 3px transparent; border-bottom: 3px transparent;border-right: 10px transparent;border-left: 10px transparent;}"); solve->setEnabled(false); if (!solvable(grid,n1,n2)) { editdlg->setStyleSheet("QWidget { background-color: brown;border-style: outset; border-width: 2px;border-radius: 10px;border-color: beige;font: bold 14px;min-width: 10em;padding: 6px; }"); editdlg->setModal(true); editdlg->show(); } } }
void solveReps() { unsigned int i; unsigned int repTime; int counter = 0; boolean flag; //debugging(30514); extraEdgeNum = num_ed + 1; for ( i = 1; i <= num_ed; i++ ) { repTime = solvable ( i ); if ( repTime == 0 ) { continue; } flag = interferingCheck ( i, repTime ); if ( flag ) { continue; } split1edge ( i, repTime ); counter ++; //+= 2*(repTime-1); if ( EdSmallerThanTwin ( i ) ) { i++; } } printf ( "%d repeats solvable, %d more edges\n", counter, extraEdgeNum - 1 - num_ed ); num_ed = extraEdgeNum - 1; removeDeadArcs(); if ( markersArray ) { free ( ( void * ) markersArray ); markersArray = NULL; } }
void Board::newGame() { //kdDebug() << "NewGame" << endl; int i, x, y, k; mark_x = -1; mark_y = -1; highlighted_tile = -1; // will clear previous highlight _undo.clear(); _redo.clear(); connection.clear(); // distribute all tiles on board int cur_tile = 1; for(y = 0; y < y_tiles(); y += 4) { for(x = 0; x < x_tiles(); ++x) { for(k = 0; k < 4 && y + k < y_tiles(); k++) setField(x, y + k, cur_tile); cur_tile++; if(cur_tile > TileSet::nTiles) cur_tile = 1; } } if(getShuffle() == 0) { update(); starttime = time((time_t *)0); emit changed(); return; } // shuffle the field int tx = x_tiles(); int ty = y_tiles(); for(i = 0; i < x_tiles() * y_tiles() * getShuffle(); i++) { int x1 = random.getLong(tx); int y1 = random.getLong(ty); int x2 = random.getLong(tx); int y2 = random.getLong(ty); int t = getField(x1, y1); setField(x1, y1, getField(x2, y2)); setField(x2, y2, t); } // do not make solvable if _solvable_flag is false if(!_solvable_flag) { update(); starttime = time((time_t *)0); emit changed(); return; } int fsize = x_tiles() * y_tiles() * sizeof(int); int *oldfield = new int[x_tiles() * y_tiles()]; memcpy(oldfield, field, fsize); // save field int *tiles = new int[x_tiles() * y_tiles()]; int *pos = new int[x_tiles() * y_tiles()]; while(!solvable(true)) { //kdDebug() << "Not solvable" << endl; //dumpBoard(); // generate a list of free tiles and positions int num_tiles = 0; for(i = 0; i < x_tiles() * y_tiles(); i++) if(field[i] != EMPTY) { pos[num_tiles] = i; tiles[num_tiles] = field[i]; num_tiles++; } // restore field memcpy(field, oldfield, fsize); // redistribute unsolved tiles while(num_tiles > 0) { // get a random tile int r1 = random.getLong(num_tiles); int r2 = random.getLong(num_tiles); int tile = tiles[r1]; int apos = pos[r2]; // truncate list tiles[r1] = tiles[num_tiles-1]; pos[r2] = pos[num_tiles-1]; num_tiles--; // put this tile on the new position field[apos] = tile; } // remember field memcpy(oldfield, field, fsize); } // restore field memcpy(field, oldfield, fsize); delete tiles; delete pos; delete oldfield; update(); starttime = time((time_t *)0); emit changed(); }
void Board::newGame() { int i, j, x, y, k; mark_x = -1; mark_y = -1; while(_undo.count()) _undo.removeFirst(); while(_redo.count()) _redo.removeFirst(); clearHistory(); for(i = 0; i < x_tiles(); i++) for(j = 0; j < y_tiles(); j++) setField(i, j, EMPTY); // distribute all tiles on board int cur_tile = 0; for(i = 0; i < x_tiles() * y_tiles() * 12; i++) { // map the tileindex to a tile // not all tiles from the pixmap are really used, only // 36 out of 45 are used. This maps and index to // the "real" index. int tile; if(cur_tile == 28) tile = 30; else if(cur_tile >= 29 && cur_tile <= 35) tile = cur_tile + 7; else tile = cur_tile; cur_tile++; if(cur_tile == 36) cur_tile = 0; x = i % x_tiles(); y = i / x_tiles() * 4; tile++; for(k = 0; k < 4 && k + y < y_tiles(); k++) setField(x, y+k, tile); } if(getShuffle() == 0) { if(!trying) { update(); starttime = time((time_t *)0); emit changed(); } return; } // shuffle the field int tx = x_tiles(); int ty = y_tiles(); for(i = 0; i < x_tiles() * y_tiles() * getShuffle(); i++) { int x1 = random(tx); int y1 = random(ty); int x2 = random(tx); int y2 = random(ty); int t = getField(x1, y1); setField(x1, y1, getField(x2, y2)); setField(x2, y2, t); } // do not make solvable if _solvable_flag is FALSE if(!_solvable_flag) { if(!trying) { update(); starttime = time((time_t *)0); emit changed(); } return; } int fsize = x_tiles() * y_tiles() * sizeof(int); int *oldfield = new int[x_tiles() * y_tiles()]; memcpy(oldfield, field, fsize); // save field int *tiles = new int[x_tiles() * y_tiles()]; int *pos = new int[x_tiles() * y_tiles()]; while(!solvable(TRUE)) { // generate a list of free tiles and positions int num_tiles = 0; for(i = 0; i < x_tiles() * y_tiles(); i++) if(field[i] != EMPTY) { pos[num_tiles] = i; tiles[num_tiles] = field[i]; num_tiles++; } // restore field memcpy(field, oldfield, fsize); // redistribute unsolved tiles while(num_tiles > 0) { // get a random tile int r1 = random(num_tiles); int r2 = random(num_tiles); int tile = tiles[r1]; int apos = pos[r2]; // truncate list tiles[r1] = tiles[num_tiles-1]; pos[r2] = pos[num_tiles-1]; num_tiles--; // put this tile on the new position field[apos] = tile; } // remember field memcpy(oldfield, field, fsize); } // restore field memcpy(field, oldfield, fsize); delete tiles; delete pos; delete oldfield; if(!trying) { update(); starttime = time((time_t *)0); emit changed(); } }