Xiao Ming a 5 people in a bridge at night, Xiao Ming Bridge to 1 minutes, Xiao Ming's brother Bridge to 3 minutes, Xiao Ming's father Bridge to 6 minutes, Xiao Ming's mother bridge to 8 minutes, Xiao Ming's grandfather Bridge to 12 minutes; This bridge can only be 2 people, because it is the night, bridge must carry the lamp, Xiaoming has a lamp, Lit after 30 minutes will be extinguished, ask how to arrange, to ensure that Xiao Ming's family before the lights go off the bridge.
Wrote a PHP program to solve this kind of problem. There are questions, welcome to point out.
<?PHP/*Bridge Problem*/main ();functionMain () {$arr=Array(3,1,12,8,6); PrintBridge$arr);}functionBridge$arr){ $route=""; if(Count($arr) ==1){ $route.=$arr[0]; } ElseIf(Count($arr) ==2){ $route.=$arr[0]>$arr[1]?$arr[0]:$arr[1]; }Else{ Sort($arr); $route.=$arr[1]. " = "; $route.=$arr[0]. " = "; $ARRTMP=$arr; unset($ARRTMP[0]); unset($ARRTMP[1]); Sort($ARRTMP); while(Count($ARRTMP) >1){ //Key if($ARRTMP[Count($ARRTMP) -2]+$arr[0]>2*$arr[1]){ $route.=$ARRTMP[Count($ARRTMP)-1]. " = "; $route.=$arr[1]. " = "; $route.=$arr[1]. " = "; $route.=$arr[0]. " = "; }Else{ $route.=$ARRTMP[Count($ARRTMP)-1]. " = "; $route.=$arr[0]. " = "; $route.=$ARRTMP[Count($ARRTMP)-2]. " = "; $route.=$arr[0]. " = "; } unset($ARRTMP[Count($ARRTMP)-2]); unset($ARRTMP[Count($ARRTMP)]); } if(Count($ARRTMP) ==1){ $route.=$ARRTMP[Count($ARRTMP)-1]; }Else{ $route=substr($route, 0,strlen($route)-strlen("= =".)$arr[0]. " = ")); } return $route; }}
Bridge the problem, ask for the shortest time