//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void TForm1::main() { char *stime[30],*etime[30]; __int64 tick1,tick2; tick1 = GetTickCount(); Form1->Cursor = crHourGlass; fid = fopen("log.txt","wt"); time(&t1); *stime = ctime(&t1); fprintf(fid,"*** pazul started : %s \n",*stime); Edit1->Text = *stime; Edit3->Text = IntToStr(time(&t2)-t1); Form1->Refresh(); exec(); tick2 = GetTickCount(); result = (int) (tick2 -tick1); time(&t2); *etime = ctime(&t2); fprintf(fid,"\n*** pazul ended time = %d ms : %s \n",result,*etime); Edit2->Text = *etime; Edit3->Text = IntToStr(t2-t1); Edit4->Text = IntToStr(result); fclose(fid); // reset cursor mark Form1->Cursor = crDefault; } //--------------------------------------------------------------------------- void TForm1::exec() { int k; if ((Edit5->Text == "") | (Edit6->Text == "")) { fprintf(fid,"**** M,N undefined ****\n"); return; } M = StrToInt(Edit5->Text); N = StrToInt(Edit6->Text); MN = M * N; if (MN > 4096) { fprintf(fid,"**** Too large problem ****\n"); return; } Lightsout = (CheckBox1->Checked == true); TurnOver8 = (CheckBox2->Checked == true); if (Lightsout & TurnOver8) { fprintf(fid,"**** Switch Conflicted ****\n"); return; } if (!(Lightsout | TurnOver8)) { fprintf(fid,"**** No Switch Selected ****\n"); return; } Col= (MN+31) >> 5; for (k=0; k<32; k++) bit[k] = 0x80000000 >> k; init(); printResult(0); solve(); printResult(1); } //--------------------------------------------------------------------------- void TForm1::init() { int k,p,q,i; for (k=0; k 0) biton(X[k],k-1); if (p < (M-1)) biton(X[k],k+N); if (p > 0) biton(X[k],k-N); } if (TurnOver8 != 0) { if (q < (N-1)) biton(X[k],k+1); if (q > 0) biton(X[k],k-1); if (p < (M-1)) biton(X[k],k+N); if (p > 0) biton(X[k],k-N); if ((p > 0) & (q > 0)) biton(X[k],k-1-N); if ((p > 0) & (q < (N-1))) biton(X[k],k+1-N); if ((p < (M-1)) & (q > 0)) biton(X[k],k-1+N); if ((p < (M-1)) & (q < (N-1))) biton(X[k],k+1+N); } } } //--------------------------------------------------------------------------- void TForm1::biton(int X[],int pos) { int i,j; i = pos >> 5; j = pos & 0x1f; X[i] |= bit[j]; } //--------------------------------------------------------------------------- int TForm1::bittest(int X[],int pos) { int i,j; i = pos >> 5; j = pos & 0x1f; return (X[i] & bit[j]); } //--------------------------------------------------------------------------- void TForm1::swap(int X[],int Y[]) { int i,w; for (i=0; i=0; k--) { if (X[k][Col] != 0) break; if (V[k][Col] != 0) { fprintf(fid,"*** Sorry, (%d,%d) has No Solution ***\n",M,N); return; } if ((MN-k) > 24) { f = 0; Q = 0; fprintf(fid,"*** (%d,%d) has Too many Frees, Non-optimal Solution ***\n",M,N); goto answer; } X[k][Col] = bit[32-(MN-k)]; } f = MN - k - 1; p = (k+1) >> 5; s = (MN - 1) & 0x1f; for (kk=0; kk<=k; kk++) { w = X[kk][Col-1]; X[kk][Col] = w >> (31-s); if (p != (Col-1)) X[kk][Col] |= X[kk][Col-2] << (s+1); } //find min solution push = MN; for (q=0; q