Test instructions: Give you three boxes, there are three kinds of bottles in each box B G C, let you classify them, so that there is only one bottle in each box,
Only one bottle can be moved at a time, requiring the minimum number of steps to be moved, the same as the dictionary output
Train of thought: order to get sequence has: BCG BGC CBG CGB GBC GCB Six kinds, all calculate by size to find on the line
PS: The order of input is: B G C, for convenience I sort of b C g
The code is as follows:
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 intdate[3][3],sum[6];6 intmins,p;7 stringtype[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};8 BOOLDatecin ()//bottle b C G per box9 {Ten if(SCANF ("%d%d%d%d%d%d%d%d%d", &date[0][0],&date[0][2],&date[0][1],&date[1][0],&date[1][2],&date[1][1],&date[2][0],&date[2][2],&date[2][1])!=EOF) One return true; A return false; - } - the voiddatecal () - { - -sum[0]=date[1][0]+date[2][0]+ date[0][1]+date[2][1]+ date[0][2]+date[1][2];//BCG +sum[1]=date[1][0]+date[2][0]+ date[0][2]+date[2][2]+ date[0][1]+date[1][1];//BGC - +sum[2]=date[1][1]+date[2][1]+ date[0][0]+date[2][0]+ date[0][2]+date[1][2];//CBG Asum[3]=date[1][1]+date[2][1]+ date[0][2]+date[2][2]+ date[0][0]+date[1][0];//CGB at -sum[4]=date[1][2]+date[2][2]+ date[0][0]+date[2][0]+ date[0][1]+date[1][1];//GBC -sum[5]=date[1][2]+date[2][2]+ date[0][1]+date[2][1]+ date[0][0]+date[1][0];//GCB - -mins=sum[0],p=0; - for(intI=1;i<6; i++)if(Mins>sum[i]) {mins=sum[i],p=i;} in } - to voidshowres () + { - datecal (); thecout<<type[p]<<' '<<sum[p]<<Endl; * } $ Panax Notoginseng intMain () - { the while(Datecin ()) + { A showres (); the } + return 0; -}
UVA, 102 Ecological Bin Packing