Fun algorithm-Frog across the river (JAVA)

Source: Internet
Author: User

Frogs cross the river is a very interesting intellectual game, the main idea is as follows: A river between a number of stone spacing, two teams of frogs across the river, each team has 3 frogs, these frogs can only move forward, can not move backwards, and only one frog at a time to move forward. During the move, the frog can move in front of the empty space, not to skip two positions at a time, but can skip the other side a frog into the front of a vacancy. Ask the two teams how to move the frog to use the fewest steps to the opposite shore separately? (→→→-←←←) Maybe 3 frogs are too few, and it's not hard to do it mentally. What if there were 100 frogs?

/*** Frogs cross the river *@authorRubekid **/ Public classRiverfrog { Public Static Final intLeft_frog =-1;  Public Static Final intRight_frog = 1;  Public Static Final intSTONE = 0; Private int[] frogs; Private intZeroindex; Private intlength; Private intStep = 0;  PublicRiverfrog (intNumber ) {Frogs=New int[Number * 2 +1]; Length=frogs.length; Zeroindex= LENGTH/2;  for(inti=0; i< number; i++) {Frogs[i]=Left_frog; } Frogs[zeroindex]=STONE;  for(inti=0; i< number; i++) {frogs[i+ Zeroindex + 1] =Right_frog; }            }         Public voidrun () { while(!ismoveend (Left_frog) | |!ismoveend (Right_frog)) {            intleft = ZeroIndex-1; intright = Zeroindex+1; if(Left>-1 && Right <length) {                if(Frogs[left]! =Frogs[right]) {                    if(Frogs[left] = =Left_frog) {                        if(Left > 0 && frogs[left-1] = = Right_frog) {//If you move right, there are two in the middle                             This. Move (right); }                        Else{                             This. Move (left); }                    }                    Else if(Left > 0 && frogs[left-1]==Left_frog) {                         This. Move (left-1); }                    Else if(Right <= length && frogs[right+1] = =Right_frog) {                         This. Move (right+1); }                }                Else{                    if(Frogs[left] = =Right_frog) {                        if(Left > 0 && frogs[left-1] = =Left_frog) {                             This. Move (left-1); }                        Else if(right+1 < length && frogs[right+1] = =Right_frog) {                             This. Move (right+1); }                        Else if(Frogs[right] = =Right_frog) {                             This. Move (right); }                    }                    Else if(Frogs[right] = =Left_frog) {                        if(right+1 < length && frogs[right+1] = =Right_frog) {                             This. Move (right + 1); }                        Else if(left >0 && frogs[left-1] = =Left_frog) {                             This. Move (left-1); }                        Else if(Frogs[left] = =Left_frog) {                             This. Move (left); }                    }                }            }            Else if(left = =-1){                if(Frogs[right] = = Left_frog && right<length-1){                     This. Move (right+1); }                Else{                     This. Move (right); }            }            Else if(right = =length) {                if(Frogs[left] = = Right_frog && Left > 0){                     This. Move (left-1); }                Else{                     This. Move (left); }}} System.out.println ("Step:" +step); }        Private voidMoveinti) {        inttemp =Frogs[i]; Frogs[i]=Frogs[zeroindex]; Frogs[zeroindex]=temp; Zeroindex=i; Step++;    Print (); }        Private BooleanIsmoveend (intvalue) {        inti=0;intmax=Zeroindex; if(Value = =Left_frog) {i= Zeroindex+1; Max=length; }         for(intJ=i; j<max; J + +){            if(frogs[j]!=value) {                return false; }        }        return true; }        Private voidprint () {StringBuffer StringBuffer=NewStringBuffer ();  for(intfrog:frogs) {            if(frog>-1) {stringbuffer.append ("" +frog + ""); }            Else{stringbuffer.append (Frog+ "    ");    }} System.out.println (Stringbuffer.tostring ()); }}

Fun algorithm-Frog across the river (JAVA)

Related Article

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.