Frog crossing the river program and its analysis

Source: Internet
Author: User

Question: seven stones on a creek ,:

There are six frogs: A, B, C, D, E, F. Three frogs A, B, and C want to go to the right bank. They only jump from left to right. Three frogs D, E, and F want to go to the left bank. They only jump from right to left. Each time a frog jumps to the front of his 2nd rock heads at most. I 'd like to know how many times all frogs will jump ashore. Write steps.

This is a path search problem. Search all solutions in the solution space and find the optimal solution (that is, the least step ).
So how can this be considered a solution? Specifically, there is no frog on the last stone.

Let's first model the problem. There are seven stones on which there can be no frogs, either a left-jumping frog or a right-Jumping Frog. The Seven Stones can be regarded as a whole to indicate a state. Here we regard the Seven Stones as an array, which can only have values 0, 1, and 2. This indicates that the initial time is:

1, 1, 0, 2, 2

We will convert it into a number to represent the State value. This value will combine the array into a number in three-digit format. We will use an auxiliary function to do this:

Private Final int makes (){

Int r = 0;
Int p = 1;
For (INT I = 0; I <7; I ++)
{
R + = p * States [I];
P * = 3;
}
Return R;
}

Now the question changes from status 111022 to status 0000000. The minimum steps are required.

How is the status converted?
Obviously ., Every frog jump triggers a state transition. We search for each possible transition in each State, and remember that the initial state is S (S equals to triplicate 111022) the value to be solved is opt (s). If it can be converted to T1, T2 ,... TK.
Then, obviously

Opt (S) = min (1 + OPT (T1), 1 + OPT (T2),..., 1 + OPT (TK ));

In addition, because the final state is 0, so opt (0) = 0, that is, it is already in the final state, and it does not need to be a step.
With the above equation, we can solve it recursively. However, if we simply do this, it will lead to a large number of repeated computations. So here we use the memorandum method, write down the obtained OPT (X) and put it in an array. Since there are only seven stones, we need up to 3 ^ 7 = 2187 states. We use an array of 2187 elements, where the I-th element represents OPT (I), and each element is initialized with-1 to indicate that it has not been solved. Opt (0) can be directly initialized to 0.

Now we have another question: how can we print out the optimal steps at the end of the algorithm? Following this step, we can reconstruct how frogs cross the river in optimal conditions. To this end, we can use a step array to record each time we take the optimal step.

The entire algorithm is as follows:

Package test;

Import java. util. arrays;
/**
*
* @ Author yovn
*
*/
Public class frogjump {

Private int steps [];
Private int States [];


Private Static class step
{
Int offset =-1;
Int jump;
Int jumpto;
}


Private step jumps [];
Private int inits;
Public frogjump ()
{
Steps = new int [81*27];
States = new int [7];
For (INT I = 0; I <3; I ++) States [I] = 1;
For (INT I = 4; I <7; I ++) States [I] = 2;
Arrays. Fill (steps,-1 );
Steps [0] = 0;
Jumps = new step [81*27];
Inits = makes ();
}

Public int shorteststeps (int s)
{
If (steps [s] =-1)
{

Int minstep = integer. max_value;
Step onestep = new step ();
For (INT I = 0; I <7; I ++)
{
If (States [I] = 1)
{
If (I> 4)
{
States [I] = 0;
Minstep = recurfind (minstep, onestep, I, 7-i );
States [I] = 1;
}
Else
{
If (States [I + 1] = 0)
{
States [I] = 0;
States [I + 1] = 1;
Minstep = recurfind (minstep, onestep, I, 1 );
States [I] = 1;
States [I + 1] = 0;

}
If (States [I + 2] = 0)
{
States [I] = 0;
States [I + 2] = 1;
Minstep = recurfind (minstep, onestep, I, 2 );
States [I] = 1;
States [I + 2] = 0;

}
}
}
Else if (States [I] = 2)
{
If (I <2)
{
States [I] = 0;

Minstep = recurfind (minstep, onestep, I,-1-I );
States [I] = 2;
}
Else
{
If (States [I-1] = 0)
{
States [I] = 0;
States [I-1] = 2;
Minstep = recurfind (minstep, onestep, I,-1 );
States [I] = 2;
States [I-1] = 0;

}
If (States [I-2] = 0)
{
States [I] = 0;
States [I-2] = 2;
Minstep = recurfind (minstep, onestep, I,-2 );
States [I] = 2;
States [I-2] = 0;

}
}
}

}
Steps [s] = minstep;
Jumps [s] = onestep;


}
Return steps [s];

}

Private Final int recurfind (INT minstep, step onestep, int POs, int jump ){
Int TOS = makes ();
Int r = shorteststeps (ToS );
If (r <minStep-1)
{
Onestep. Jump = jump;
Onestep. offset = Pos;
Onestep. jumpto = TOS;
Minstep = R + 1;
}
Return minstep;
}



Public void printpath ()
{
Int S = inits;
Int I = 1;

While (s! = 0)
{


System. out. println ("[" + (I ++) + "] frog at #" + jumps [s]. offset + "jumps #" + jumps [s]. jump );
S = jumps [s]. jumpto;

}
}
Private Final int makes (){

Int r = 0;
Int p = 1;
For (INT I = 0; I <7; I ++)
{
R + = p * States [I];
P * = 3;
}
Return R;
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Frogjump Fj = new frogjump ();
Int steps = FJ. shorteststeps (FJ. inits );

System. Out. println ("use" + steps + "steps! ");
FJ. printpath ();

}

}

Running result: (#1 indicates a jump to the right, #-2 indicates a jump to the left .)

Use 21 steps!
[1] frog at #2 jumps #1
[2] frog at #4 jumps #-2
[3] frog at #5 jumps #-1
[4] frog at #3 jumps #2
[5] frog at #1 jumps #2
[6] frog at #0 jumps #1
[7] frog at #2 jumps #-2
[8] frog at #0 jumps #-1
[9] frog at #4 jumps #-2
[10] frog at #2 jumps #-2
[11] frog at #0 jumps #-1
[12] frog at #5 jumps #2
[13] frog at #3 jumps #2
[14] frog at #1 jumps #2
[15] frog at #5 jumps #2
[16] frog at #3 jumps #2
[17] frog at #5 jumps #2
[18] frog at #6 jumps #-1
[19] frog at #5 jumps #-2
[20] frog at #3 jumps #-2
[21] frog at #1 jumps #-2

 

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.