//--------------------------------------------------------------------------- void __fastcall TFormSolvePuzzle::ButtonSolveClick(TObject *Sender) { const std::vector<PuzzlePiece> piecesOriginal = GetPieces(); std::vector<PuzzlePiece> pieces1d = piecesOriginal; std::sort(pieces1d.begin(),pieces1d.end()); ProgressBar->Max = IntPower(4,9); ProgressBar->Hint = ProgressBar->Max; const int maxI = IntPower(4,9); for (int i = 1; i!=maxI; ++i) { const std::vector<PuzzlePiece> solution = SolvePuzzle(std::vector<PuzzlePiece>(),pieces1d); if (solution.empty() == false) { ShowSolution(solution); ShowMessage("Solved!"); return; } Application->ProcessMessages(); if (mQuit == true) return; ProgressBar->Position = i; //Rotate a piece or more pieces for (int index = 0; index != 9; ++index) { pieces1d[index].Rotate(); if (pieces1d[index].mNrotations != 0) break; } } ShowMessage("Not solved..."); }
int IntPower(int Base, int Exponent) { if(Exponent==0) return(1); else return(Base*IntPower(Base, Exponent-1)); }
//=========================================================================== void TPolinom::Run() { if (!isEnabledCalc()) return; TElement::Run(); // -------------- TFloat *Queue = oPoints->Items[0]->Queue; Queue[0] = Polinom[0]; for (int i = 1; i < 6; i++) if (Polinom[i] != 0) Queue[0] += Polinom[i] * IntPower(*pCurModelTime, i); }
//--------------------------------------------------------------------------- __fastcall TFormSolvePuzzle::TFormSolvePuzzle(TComponent* Owner) : TForm(Owner), mQuit(false) { //Tests assert(IntPower(2,2)== 4); assert(IntPower(2,3)== 8); assert(IntPower(2,4)==16); assert(IntPower(3,2)== 9); assert(IntPower(3,3)==27); assert(IntPower(3,4)==81); //More tests PuzzlePiece p0( greenBeetleHead, greenBeetleTail, brownBeetleHead, brownBeetleTail); PuzzlePiece p1 = p0; assert(p0 == p1); p1.Rotate(); assert(p0 != p1); PuzzlePiece p2 = p1; assert(p1 == p2); p2.Rotate(); assert(p1 != p2); PuzzlePiece p3 = p2; assert(p2 == p3); p3.Rotate(); assert(p2 != p3); PuzzlePiece p4 = p3; assert(p3 == p4); p4.Rotate(); assert(p3 != p4); assert(p0 == p4); }
int FinaSum(int pnum1,int pnum2) { int j = 0,first = 0,second = 0,sum = 0,count = 0; int base = 3; first = pnum1; second = first + 1; count = pnum2 - pnum1; for(j = 0;j < count;j++) { sum = first + second; first = second; second = sum ; } IntPower(base,pnum1); return sum; }