The JAVA Implementation of the big lion is a, the little lion is a, the big tiger is B, the little tiger is B, the Big Wolf is C, the little wolf is C, the problem of crossing the river together

Source: Internet
Author: User

/**
*
* @ Author z840000001
* A long time ago, there were lions, tigers, and wolves in a forest. They wanted to visit the elephant house at the invitation of the elephant. A small river stops you on the way. You need to cross the river. There is only one canoe. Currently known:
* 1. There are 6 in total, namely, the father of the lion, the little lion, the father of the tiger, the tiger, the father of the wolf, and the wolf.
* 2. The little guys are relatively weak. If they are not covered by their fathers, they will be eaten by the big guys next to them.
* 3. A canoe can sit at most two people, regardless of the size of each.
* 4. Big guys will all go boating. Only the tigers in the little guy will go boating.
* Method of crossing the river safely.
* Tip: you can set a as a, a as A, B as a tiger, B as a tiger, C as a wolf, and C as a wolf. Then, the image uses a vertical line to represent the river, and 0 to represent the ship. The question is to draw a line.
* Aabbcc | 0 |
*...
* | 0 | aabbcc
* Steps omitted...
*/

 

Import java. util. vector;

Public class extends sstheriver {

Private Static final int ship = 0;
Private Static final int lion_a = 1;
Private Static final int lion_a = 2;
Private Static final int tiger_ B = 3;
Private Static final int tiger_ B = 4;
Private Static final int wolf_c = 5;
Private Static final int wolf_c = 6;
Private Static final int total_status = 7;
 
// Use an integer int to store the current status. 0 indicates that the ship is on the left bank, and 1 indicates that the ship is on the right bank.
// 0 indicates that lion_a is on the left bank, and 1 indicates that lion_a is on the right bank.
// 0 indicates that lion_a is on the left bank, 1 indicates that lion_a is on the right bank, and so on.
Private Static final int begin_status = 0; // The initial status is all on the Left Bank.
Private Static final int end_status = 0x7f; // The destination is to go to the right bank.
Private Static final int sailor = (1 <lion_a) | (1 <tiger_ B) | (1 <tiger_ B) | (1 <wolf_c );
Private Static vector <integer> stepvec = new vector <integer> (); // The step for storing the ferry.

// Store the next possible scenario for each scenario
// Considering a total of 7 objects in a ship with six animals and one ship, each object has two States on the Left Bank and the right bank. Therefore, there are a total of 128 7-power scenarios.
// There are only several possible next steps for each scenario: for example, AABB | 0 | next step of CC, there are only two valid scenarios: AA | 0 | bbcc and BB | 0 | AACC.
// Using this table, you can easily predict the next action and deduct it.
Private Static vector <integer> [] tracemap = createtracemap ();

// Create tracemap
Private Static vector <integer> [] createtracemap (){
// Create a scenario table tracemap
Vector <integer> [] tracemaptmp = new vector [1 <total_status];
// Int icount = 0;
For (INT I = 0; I <(1 <total_status); I ++ ){
Tracemaptmp [I] = new vector <integer> ();
If (! Isstatuslegal (I) {// if the current status is invalid, you do not need to check it again.
Continue;
}
// Printstatus (I );
Int statustmp;
If (isright (I, ship) {// if the ship is on the right bank
For (Int J = 1; j <total_status; j ++ ){
If (isright (I, j) & issailor (j )){
// Decide the boating
Statustmp = setleft (I, ship), J );
If (isstatuslegal (statustmp )){
Tracemaptmp [I]. Add (New INTEGER (statustmp ));
// Icount ++;
// Printstatus (statustmp );
}
// Determines whether to bring passengers
Int statustmp2;
For (int K = 1; k <total_status; k ++ ){
If (isright (statustmp, k )){
Statustmp2 = setleft (statustmp, k );
If (isstatuslegal (statustmp2 )){
Tracemaptmp [I]. Add (New INTEGER (statustmp2 ));
// Icount ++;
// Printstatus (statustmp2 );
}
}
}
}
}
} Else {// if the ship is on the left bank
For (Int J = 1; j <total_status; j ++ ){
If (! Isright (I, j) & issailor (j )){
// Decide the boating
Statustmp = setright (I, ship), J );
If (isstatuslegal (statustmp )){
Tracemaptmp [I]. Add (New INTEGER (statustmp ));
// Icount ++;
// Printstatus (statustmp );
}
// Determines whether to bring passengers
Int statustmp2;
For (int K = 1; k <total_status; k ++ ){
If (! Isright (statustmp, k )){
Statustmp2 = setright (statustmp, k );
If (isstatuslegal (statustmp2 )){
Tracemaptmp [I]. Add (New INTEGER (statustmp2 ));
// Icount ++;
// Printstatus (statustmp2 );
}
}
}
}
}
}
// System. Out. println ("\ n ");
}
// System. Out. println ("icount =" + icount );
Return tracemaptmp;
} // End of createtracemap
 
// Ferry
Private Static Boolean gotowards (INT status ){
// Determine whether the game is successful
If (issucceed (Status )){
Stepvec. Add (New INTEGER (Status ));
Return true;
}
If (stepvec. indexof (Status) =-1) {// if it is a scenario that has never been met before, continue to look at it.
Stepvec. Add (New INTEGER (Status ));
} Else {// if you have switched back to the previous scenario, give up this method.
Return false;
}
For (INT I = 0; I <tracemap [Status]. Size (); I ++) {// continue analysis for every possible next scenario
If (gotowards (tracemap [Status]. elementat (I). intvalue () {// Recursion
Return true;
}
}
Stepvec. Remove (stepvec. Size ()-1); // if not found
Return false;
}
 
// Determine the conditions for victory
Private Static Boolean issucceed (INT status ){
Return (status = end_status )? True: false;
}
// Determines whether a boat will be used.
Private Static Boolean issailor (INT animal ){
Return (sailor & (1 <animal) = (1 <animal );
}
// Check whether the animal numbered animal is on the right bank in status.
Private Static Boolean isright (INT status, int animal ){
Return (Status & (1 <animal) = (1 <animal );
}
// Move a beast to the left
Private Static int setleft (INT status, int animal ){
Return Status &((~ 0) ^ (1 <animal ));
}
// Move a beast to the right
Private Static int setright (INT status, int animal ){
Return status | (1 <animal );
}
 
// Determine whether there are any animals in the current situation as victims
Private Static Boolean isstatuslegal (INT status ){
For (INT I = 1; I <total_status; I + = 2 ){
While (true ){
// If the parent and child are not on the same shore
If (isright (status, I) ^ isright (status, I + 1 )){
// The other two beasts must be on the same side as their father.
If (isright (status, (I + 2) % 6) ^ isright (status, I + 1 ))
& (Isright (status, (I + 4) % 6) ^ isright (status, I + 1 ))){
Break;
}
// Otherwise, the beast will be suspended.
Return false;

// If the father and son are on the same shore, there will be plenty of beasts to protect, and little beasts will be okay.
} Else {
Break;
}
}
}
Return true;
}
 
// Print the current status
Private Static void printstatus (INT status ){
Stringbuffer strtmp = new stringbuffer ("aabbcc | 0 0 | aabbcc ");
If (isright (status, ship )){
Strtmp. Replace (7, 8 ,"");
} Else {
Strtmp. Replace (10, 11 ,"");
}
For (INT I = 1; I <total_status; I ++ ){
If (isright (status, I )){
Strtmp. Replace (I-1, I ,"");
} Else {
Strtmp. Replace (I + 11, I + 12 ,"");
}
}
System. Out. println (strtmp );
}
 
// Print the step result
Private Static void showstep (){
Gotowards (begin_status );
For (INT I = 0; I <stepvec. Size (); I ++ ){
Printstatus (stepvec. elementat (I). intvalue ());
}
}
 
Public static void main (string [] ARGs ){
Showstep ();
}
}

 

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.