Bridge the problem, ask for the shortest time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.