Java programming capability enhancement-wolf sheep crossing the river

Source: Internet
Author: User

Question: There are three wolves and three lambs to cross the river. There is only one boat. at most two animals can be taken at a time, if the number of wolves on one side is greater than that on the other side, the sheep will be eaten and programmed to provide solutions.

Reference answer:

Package packages;

Public class LangAndYang {

Public static void main (String [] args ){
Int state [] = {3 };
// 1st. Two elements indicate the number of wolves and sheep on the Left Bank.
New LangAndYang (). next (state, null );
}
 
Public void next (int state [], StringBuffer str ){
Int [] newState;
If (str = null) {// indicates the first step
// A wolf and a sheep
NewState = move (state, "-1-1 ");
Next (newState, new StringBuffer ("-1-1 "));
// The two wolves cross the river
NewState = move (state, "-2-0 ");
Next (newState, new StringBuffer ("-2-0 "));
Return;
}

If (state [0] = 0 & state [1] = 0) {// All are transferred to the right bank
PrintResult (str );
Return;
}

If (str! = Null & hasExist (str) {// check whether it is an endless loop
Return;
}

// Consider transferring to the right
If (str. charAt (0) ==+ ){
// Two wolves
If (state [0]> = 2 &&! Str. substring (0, 4). equals ("+ 2 + 0 ")){
NewState = move (state, "-2-0 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "-2-0 "));
}
}
// A wolf
If (state [0]> = 1 &&! Str. substring (0, 4). equals ("+ 1 + 0 ")){
NewState = move (state, "-1-0 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "-1-0 "));
}
}
// A goat
If (state [1]> = 1 &&! Str. substring (0, 4). equals ("+ 0 + 1 ")){
NewState = move (state, "-0-1 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "-0-1 "));
}
}
// A wolf and a sheep
If (state [0]> = 1 & state [1]> = 1 &&! Str. substring (0, 4). equals ("+ 1 + 1 ")){
NewState = move (state, "-1-1 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "-1-1 "));
}
}
// Two lambs
If (state [1]> = 2 &&! Str. substring (0, 4). equals ("+ 0 + 2 ")){
NewState = move (state, "-0-2 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "-0-2 "));
}
}
} Else {// consider moving left
// Two wolves
If (state [0] <2 &&! Str. substring (0, 4). equals ("-2-0 ")){
NewState = move (state, "+ 2 + 0 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "+ 2 + 0 "));
}
}
// A wolf
If (state [0] <3 &&! Str. substring (0, 4). equals ("-1-0 ")){
NewState = move (state, "+ 1 + 0 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "+ 1 + 0 "));
}
}
// A goat
If (state [1] <3 &&! Str. substring (0, 4). equals ("-0-1 ")){
NewState = move (state, "+ 0 + 1 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "+ 0 + 1 "));
}
}
// A wolf and a sheep
If (state [0] <3 & state [1] <3 &&! Str. substring (0, 4). equals ("-1-1 ")){
NewState = move (state, "+ 1 + 1 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "+ 1 + 1 "));
}
}
// Two lambs
If (state [1] <2 &&! Str. substring (0, 4). equals ("-0-2 ")){
NewState = move (state, "+ 0 + 2 ");
If (check (newState )){
Next (newState, new StringBuffer (str). insert (0, "+ 0 + 2 "));
}
}
}
}
 
/*
* The first parameter indicates the status, and the second parameter indicates the routing. Use-to the right and + to the left.
* The returned value indicates the new status.
*/
Public int [] move (int state [], String info ){
Int lang = 0;
Try {
Lang = Integer. parseInt (info. substring (0, 2 ));
} Catch (Exception e ){
Lang = Integer. parseInt (info. substring (1, 2 ));
}
Int yang = 0;
Try {
Yang = Integer. parseInt (info. substring (2 ));
} Catch (Exception e ){
Yang = Integer. parseInt (info. substring (3 ));
}
Int [] result = new int [state. length];
Result [0] = state [0] + lang;
Result [1] = state [1] + yang;
Return result;
}
 
/*
* Check whether the status is suitable. the number of wolves cannot be greater than that of sheep.
*/
Public boolean check (int state []) {
If (state [0]> state [1] & state [1]> 0) {// There are sheep on the left, and there are more wolves than sheep
Return false;
} Else if (state [0] <state [1] & state [1] <3) {// There are more wolves than sheep on the right
Return false;
} Else
Return true;
}
 
/*
* To prevent endless loops, for example, first a wolf and a sheep, then a sheep, then a wolf, and then two wolves, then return to the initial state.
*/
Public boolean hasExist (StringBuffer str ){
Int langSum = 0;
Int yangSum = 0;
For (int I = 0; I <str. length ()/4; I ++ ){
If (str. charAt (I * 4) = -){
LangSum + = str. charAt (I * 4 + 1)-0;
YangSum + = str. charAt (I * 4 + 3)-0;
} Else {
LangSum-= str. charAt (I * 4 + 1)-0;
YangSum-= str. charAt (I * 4 + 3)-0;
}
If (langSum = 0 & yangSum = 0 & I % 2 = 1)
Return true;
}
Return false;
}
 
Public void printResult (StringBuffer str ){
System. out. println ("----- solution ------");
For (int I = str. length ()/4-1; I> = 0; I --){
If (str. charAt (I * 4) = -){
System. out. println ("running" + str. charAt (I * 4 + 1) + "wolf," + str. charAt (I * 4 + 3) + "goat ");
} Else {
System. out. println ("--------------- return" + str. charAt (I * 4 + 1) + "wolf," + str. charAt (I * 4 + 3) + "goat ");
}
}
System. out. println ();
}
}

I hope you understand the code, rather than saving it.

 

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.