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.
For programming ideas, refer to: Java programming capability enhancement (2) -- General Solution to search for 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.