Rendezvous with C/C++

Given 2*N  boxes in  line  side  by  side  (N<=5).  Two
adjacent boxes are empty, and the other boxes contain N-1
symbols "A" and N-1 symbols "B".

Example for N=5:

        | A | B | B | A |   |   | A | B | A | B |

Exchanging rule:

     The content  of any two adjacent non-empty boxes can
     be moved  into the  two empty ones, preserving their


     Obtain a  configuration where  all A's are placed to
     the left of all B's, no matter where the empty boxes




# include <iostream.h>
# include <stdio.h>

int getblankposition(int size, char* arrBoxes){
 if(!arrBoxes) return -1; //if the array is null return -1
 int position=0;
 for(int i=0;i<size;i++){
  if(arrBoxes[i]==' '){
   break; //break on first find
  } //end if
 } // end for
 return position;

int evaluateRank(int windowPos, int blankPos, int size, char* arrBoxes){
 // rank is evaluated on the basis of number of sequences formed
 if(!arrBoxes) return -1;
 // there will be two conditions based on where the window is with reference to the blanks
 // under no ciscumstances will both be equal
 int fCounter=0;
 int bCounter=0;
 int fCount=0;
 int bCount=0;

  // count UP the array
  }//end for

  // count DOWN the array
  }//end for
  // here windowPos<blankpos
  // count UP the array
  } //end for

  // count DOWN the array
  }//end for
 } // end if..else
 // at this point return rank
 return (bCount+fCount);

void rearrange(int size, char* arrBoxes){
 if(!arrBoxes) return; // if the array is null exit
 int blankPos; //position of the first blank
 blankPos = getblankposition(size, arrBoxes);

 if(blankPos==-1) return; //some error here
 // here we have the blank position
 // now need to find the rank for each two character window.

 int bSwitch=1; //flag to continue the loop
 int rank=0;
 int rankPos=0;

  for(int i=0;i<(size-1);i++){
   int tempRank = evaluateRank(i,blankPos, size, arrBoxes);
    // make this the new rank
    rank = tempRank;
    rankPos = i;
   }//end if
  }//end for
  // at this point, we have the position that is going to provide the best move
  // so perform the exchange
  arrBoxes[rankPos] ^= arrBoxes[blankPos];
  arrBoxes[blankPos] ^= arrBoxes[rankPos];
  arrBoxes[rankPos] ^= arrBoxes[blankPos];

  // exchange the second positions on each side
  arrBoxes[rankPos+1] ^= arrBoxes[blankPos+1];
  arrBoxes[blankPos+1] ^= arrBoxes[rankPos+1];
  arrBoxes[rankPos+1] ^= arrBoxes[blankPos+1];

  // now the blanks have moved, so set the new blank position
  blankPos = rankPos;

  // check if we need to proceed
 } //end while

Skip to main content