Ejemplo n.º 1
0
void Try(int Position_Color, 
			int Labels[], int Num_Comp, 
			int LabelsB[], int Num_Blocks, 
			int Sol[], int Num_Sol){
   int k,i;
   int continue_to_try;
   int app_Labels[Size];
   int app_LabelsB[Size];

	if (Demo==1){
   	printf("\nTRY COLOR: %d, position=%d, with Freq %d\n",Colors[Position_Color],Position_Color,Freq[Position_Color]);
   	Print_Summary(Labels,Num_Comp,LabelsB,Num_Blocks,Sol,Num_Sol);
   }
   continue_to_try=Try_Add_Color(Position_Color,
   					Labels,&Num_Comp,
					LabelsB,&Num_Blocks,
					   Sol,&Num_Sol);
   if (Num_Blocks==1){
    	for (k=0; k<Num_Sol; k++){	// := 1 to Num_Sol
   		Best_Sol[k]=Sol[k];
      }
   	Best_Num_Sol=Num_Sol;
      Best_call=call;
      printf(" \n ********************************* BEST SOL %d\n",Best_Num_Sol);
      if (Demo==1){
         Print_Summary(Labels,Num_Comp,LabelsB,Num_Blocks,Sol,Num_Sol);
      }
   }
   else{
      if (continue_to_try==1){
      	for (i=0; i<n; i++)	app_Labels[i]=Labels[i];
      	for (i=0; i<n; i++)	app_LabelsB[i]=LabelsB[i];
        	k=Position_Color+1;
        	while((k<c)&&(Num_Sol<Best_Num_Sol-1)){
         	if ((Num_Sol==Best_Num_Sol-2)&&(Freq[k]<(Num_Comp-1)))	break;
            for (i=0; i<n; i++){
         		Labels[i]=app_Labels[i];
         	}
            for (i=0; i<n; i++){
         		LabelsB[i]=app_LabelsB[i];
         	}
            Try(k,	
					Labels,Num_Comp,
					LabelsB,Num_Blocks,
					Sol,Num_Sol);
            k=k+1;
            if (Demo==1){
            	if(k<c){
            		printf(" \n Continue Inner_Try of %d, position=%d, with the next color %d, position=%d\n",Colors[Position_Color],Position_Color,Colors[k],k);
            	}
            }
         }
      }
   }
   if (Demo==1){
   	if (continue_to_try==0)	printf("----> End Try color %d, position=%d\n",Colors[Position_Color],Position_Color);
      else printf(" \n End Inner_Try of %d, position=%d, with color %d, position=%d\n",Colors[Position_Color],Position_Color,k,Colors[k]);
   	getch();
   }
}
Ejemplo n.º 2
0
void TFuncSpecView::Paint(TDC& dc, bool erase, TRect& rect)
{
  TSwitchMinApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TSwitchMinApp);
  if (theApp) {
    // Only paint if we're printing and we have something to paint, otherwise do nothing.
    //
    if (theApp->Printing && theApp->Printer && !rect.IsEmpty()) {
      // Use pageSize to get the size of the window to render into.  For a Window it's the client area,
      // for a printer it's the printer DC dimensions and for print preview it's the layout window.
      //
      TSize   pageSize(rect.right - rect.left, rect.bottom - rect.top);

      TPrintDialog::TData& printerData = theApp->Printer->GetSetup();

      // Get area we can draw in
      TRect drawingArea(swdoc->PaintHeader(dc, rect, "", 0, 0, false), rect.BottomRight());

      TEXTMETRIC tm;
      dc.SelectObject(TFont("Arial", -12));
      if (!dc.GetTextMetrics(tm)) {
        dc.SelectObject(TFont("Arial", -12));
        dc.GetTextMetrics(tm);
      }
      int fHeight=tm.tmHeight+tm.tmExternalLeading;

      unsigned long inputs=swdoc->GetSystem()->GetInputs();

      XPosVector xpos;
      uint divider;
      char* data;

      Print_CreateWidths(xpos, divider, dc);
      if (xpos.back().x < rect.Size().cx) {  // put space in structure to stretch across page
        uint spacing=(rect.Size().cx - xpos.back().x)/(xpos.size()+1);
        uint space_inc=spacing;
        for(XPosVector::size_type i=0; i<xpos.size(); i++) {
          if (i==inputs) {  // after inputs section, increase divider
            divider += space_inc;
            space_inc += spacing;
          }
          xpos[i].x += space_inc;
          space_inc += spacing;
        }
      }

      data=new char[xpos.size()];

      unsigned long PagesDown=1+pow(2,inputs)/( (drawingArea.Size().cy/fHeight) - 2);  // 2 lines for header and horz divider
      unsigned long PagesAcross=1+xpos.back().x/drawingArea.Size().cx;
      unsigned long Pages=1+PagesDown*PagesAcross;

      // Compute the number of pages to print.
      //
      printerData.MinPage = 1;
      printerData.MaxPage = Pages;


      int fromPage = printerData.FromPage == -1 ? 1 : printerData.FromPage;
      int toPage = printerData.ToPage == -1 ? 1 : printerData.ToPage;
      int currentPage = fromPage;

      TPoint p;

      dc.SelectObject(TPen(TColor::Black));

      while (currentPage <= toPage) {
        swdoc->PaintHeader(dc, rect, "Specification", currentPage, Pages, true);
        if (currentPage==1) Print_Summary(dc, drawingArea);
        else {
          // Calculate origin of line by page number
            // x
            int col=((currentPage-2) ) % PagesAcross;
            p.x=drawingArea.left - col * drawingArea.Size().cx;
            // y
            int row=floor((currentPage-2)/PagesAcross);
            p.y=drawingArea.top/* - row * drawingArea.Size().cy*/;

          // Calculate starting and ending input states
          CELL_TYPE state, stateEnd;
          state=(row*(drawingArea.Size().cy-fHeight))/fHeight;
          stateEnd=state+(drawingArea.Size().cy-fHeight)/fHeight;
          if (stateEnd >= (pow(2,inputs)-1) ) stateEnd=pow(2,inputs)-1;

          //
          // Draw header and lines

          // Vertical
          if ( ((p.x+divider) > drawingArea.left) && ((p.x+divider)<drawingArea.right) ) {
            dc.MoveTo(p.x+divider, p.y);
            dc.LineTo(p.x+divider, p.y+((stateEnd-state)+3)*fHeight);
          }

          // Labels
          for(XPosVector::size_type i=0; i<xpos.size(); i++)
            dc.TextOut(TPoint(xpos[i].x, p.y), xpos[i].s.c_str());
          p.y += fHeight;

          // Horizontal
          dc.MoveTo(drawingArea.left, p.y + fHeight/2);
          dc.LineTo(p.x+xpos.back().x, p.y + fHeight/2);
          p.y += fHeight;

          //
          // Draw each line
          while(state <= stateEnd) {
            Print_GenerateLine(data, state);
            Print_Line(dc, p, xpos, data);
            p.y += fHeight;
            state++;
          }
        }

        currentPage++;
      }

      delete[] data;
    }
    else {
      // INSERT>> Normal painting code goes here.
      wTabs->InvalidateRect(rect, erase);
    }
  }
}
Ejemplo n.º 3
0
int Try_Add_Color(	int Position_Color, 
					int Labels[], int *Num_Comp, 
					int LabelsB[], int *Num_Blocks, 
					int Sol[], int *Num_Sol){
	int i,j,k;
   Edge_T Edge;
   int Labels_support_Add[Size];
   int LabelsB_support_Add[Size];
   int added;
   int Labeli,Labelj,Labelk;
   int app_Num_Comp;
   int app_Num_Blocks;

	int path[Size];
   int Queue[Size];
   int q,Qlast;
   Adj_item *a,*aa;
   

   app_Num_Comp = *Num_Comp;
   app_Num_Blocks = *Num_Blocks;

   for (i=0; i<n; i++)	Labels_support_Add[i]=Labels[i];
   for (i=0; i<n; i++)	LabelsB_support_Add[i]=LabelsB[i];

   //call=call+1;

	if (Demo==1){
   	printf(" Adding %d, position=%d\n",Colors[Position_Color],Position_Color);
   	Print_Summary(	Labels,*Num_Comp,
	   				LabelsB,*Num_Blocks,
	   				Sol,*Num_Sol);
   }
   Edge=Edges[Position_Color];
   while (Edge!=NULL){
	 	i=Edge->u;
   		j=Edge->v;
   		
   		
   		
      if (Labels[i]!=Labels[j]){
      	*Num_Comp=*Num_Comp-1;
         Labeli=Labels[i];
         Labelj=Labels[j];
         for (k=0; k<n; k++){	// := 1 to N
         	if (Labels[k]==Labelj)
            	   Labels[k]=Labeli;
      	}
   	}
      else {																							 
			if(LabelsB[i]!=LabelsB[j]) {
				// BFS for the shortest path from i to j
      		for (k=0; k<n; k++)	
         		path[k]=n;
         	path[j]=j;
         	Queue[0]=j;
         	q = 0 ;
         	Qlast = 0 ;
         	do{
         		a = Adj[Queue[q]];
         		while (a!=NULL){
         			if (path[a->v]==n){
         				path[a->v]=Queue[q];
         				Qlast++;
         				Queue[Qlast]=a->v;
							}
         			a=a->next ;
						}
         			q++;
					} while (path[i]==n);
					// End of the BFS
         		// The path is i, path[i], path[path[i]], ..., j
         		
         		// Traverse the path from i to j joining the blocks found
      			Labeli = LabelsB[i];
         		k = i ;
      			do{
				  		k = path[k] ;
				  		if(LabelsB[k]!=Labeli) {
							*Num_Blocks = *Num_Blocks - 1;
				  			Labelk = LabelsB[k] ;
				  			for(int q=0; q<n; q++)
				  				if (LabelsB[q]==Labelk)
				  					LabelsB[q]= Labeli ;
							}
						} while (k!=j);
				} // END OF if(label_appB[p][i]!=label_appB[p][j]){
					
      }
      
     	a=new Adj_item;
			a->v=i;
        	a->c=Position_Color;
        	a->next=Adj[j];
        	Adj[j]=a;
        
      a=new Adj_item;
			a->v=j;
        	a->c=Position_Color;
        	a->next=Adj[i];
        	Adj[i]=a;
              		
  
   	Edge=Edge->Next;
   }

   if (Demo==1){
   	printf(" Num_Blocks = %d\n",*Num_Blocks);
   	printf(" Num_Comp   = %d\n",*Num_Comp);
   }
   if (((*Num_Blocks<app_Num_Blocks)&&(*Num_Sol<Best_Num_Sol-2))
      || ((*Num_Sol==Best_Num_Sol-2)&&(*Num_Blocks==1)) ) {
      *Num_Sol=*Num_Sol+1;
   	Sol[*Num_Sol-1]=Colors[Position_Color];
      call=call+1;
      added=1;
      if (Demo==1){
         printf("\n --> ADDED COLOR %d, position=%d, --> Num_Sol= %d\n",Colors[Position_Color],Position_Color,*Num_Sol);
   		Print_Summary(Labels,*Num_Comp,LabelsB,*Num_Blocks,Sol,*Num_Sol);
      	printf("----> added=%d, End Adding %d, position=%d\n",added,Colors[Position_Color],Position_Color);
   		getch();
   	}
   }
   else{
      added=0;
      if (Demo==1){
   		if ((*Num_Blocks<app_Num_Blocks)&&(*Num_Sol<Best_Num_Sol-2))	
		   		printf(" Don't add %d because it doesn't decrease the number of Blocks %d!\n",
		   		Colors[Position_Color],app_Num_Blocks);
         else	if ((*Num_Sol==Best_Num_Sol-2)&&(*Num_Blocks!=1))	
		 			printf(" Don't add %d, position=%d, because it doesn't give a solution! \n",
					 Colors[Position_Color],Position_Color);
   		Print_Summary(Labels,*Num_Comp,LabelsB,*Num_Blocks,Sol,*Num_Sol);
      	printf("----> added=%d, End Adding %d, position=%d\n",added,Colors[Position_Color],Position_Color);
   		getch();
   	}
      *Num_Comp=app_Num_Comp;
      *Num_Blocks=app_Num_Blocks;
      for (i=0; i<n; i++)	Labels[i]=Labels_support_Add[i];
      for (i=0; i<n; i++)	LabelsB[i]=LabelsB_support_Add[i];
      
      // Delete edjes with color "Position_Color"
         if (Demo==1) printf("\n Deleting color %d :",Position_Color);
         for (int i=0; i<n; i++){
         	while(Adj[i]!=NULL){
         		if (Adj[i]->c == Position_Color) {
         			a = Adj[i];
						Adj[i] = Adj[i]->next;
         	  		delete[] a;
						}
					else break;
					}
				if (Adj[i]!=NULL){
         		a = Adj[i] ;
         		while((a->next)!=NULL){
         			if ((a->next)->c == Position_Color) {
         				aa = a->next ;
							a->next = aa->next ;
         	  			delete[] aa;
         	  			}
						else 
							a=a->next;
						}
					}
				}
         if (Demo==1) printf("\n Deleted color %d :",Position_Color);


   }

   if (((call%1000)==0)&&(previous_call!=call)){
      previous_call=call;
	  	printf("call %d S[%d] ",call,*Num_Sol);
   	for (i=0; i<*Num_Sol; i++){	//i := 1 to Num_Sol
   		printf("%d ",Sol[i]);
      }
      printf("\n Blocks: %d. Comp: %d\n",*Num_Blocks,*Num_Comp);
   }

   return added;

}