题目
农夫带着狼, 羊, 白菜过河
如果农夫没有在旁边看着它们, 狼就会吃羊, 羊会吃白菜
但是小船一次只能载一人一物
请用程序打印出农夫带它们过河的流程
分析
a岸 过河 b岸
1 人 狼 羊 菜 人 羊 --->
2 狼 菜 人 羊
3 狼 菜 <--- 人 羊
4 人 狼 菜 羊
5 菜 人 狼 ---> 羊
6 菜 人 狼 羊
7 菜 <--- 人 羊 狼
8 人 羊 菜 狼
9 羊 人 菜 ---> 狼
10 羊 人 狼 菜
11 羊 <--- 人 狼 菜
12 人 羊 狼 菜
13 人 羊 ---> 狼 菜
14 人 羊 狼 菜
"farmer", "sheep", "wolf", "vegetable" 在a岸为0 在b岸为1
--------------------------------------------------------------------------------------
0 0 0 0 人狼羊菜
1 1 0 0 狼菜 人羊
0 1 0 0 人狼菜 羊
1 1 1 0 菜 人狼羊
0 0 1 0 人羊菜 狼
1 0 1 1 羊 人狼菜
0 0 1 1 人羊 狼菜
1 1 1 1 人狼羊菜
代码
import java.util.Arrays;
public class Homework0506 {
static int[] con_a = { 0, 0, 0, 0 };
static int[][] boat = { {1,1,0,0}, {1,0,1,0}, {1,0,0,1}, {1,0,0,0} };
static int[] con_b = new int[4];
static int[][] safe = {
{0, 0, 0, 0}, {1, 1, 1, 1},
{0, 0, 0, 1}, {0, 0, 1, 0},
{0, 1, 0, 0}, {0, 1, 0, 1},
{1, 1, 1, 0}, {1, 1, 0, 1},
{1, 0, 1, 1}, {1, 0, 1, 0},
};
public static void main(String[] args){
System.out.println("\t\t\t\t人\t狼\t羊\t菜");
System.out.print("初始状态:" + "\t");
array( con_a );
System.out.println("--------------开始---------------");
int count = 1;
while ( con_a[0] + con_a[1] + con_a[2] + con_a[3] != 4 ){
for (int i = 0; i < 4; i++){
System.out.println("第" + count++ + "轮坐船方案");
System.out.print("坐船:" + "\t\t");
array( boat[i] );
move(i, con_a, boat);
if ( isSafe(con_b) ){
System.out.println("该轮方案成功!");
print();
change();
System.out.println();
}else{
System.out.println("该轮方案失败,尝试下一轮方案");
}
if(con_a[0] + con_a[1] + con_a[2] + con_a[3] == 4){
break;
}
System.out.println("---------------------------------");
}
}
System.out.println("过河成功!");
}
public static void array(int[] con) {
for(int p = 0; p < 4; p++){
System.out.print("\t" + con[p]);
}
System.out.println();
}
public static void move(int i, int[] con_a, int[][] boat ){
con_b[0] = ( con_a[0] + boat[i][0] ) % 2;
con_b[1] = ( con_a[1] + boat[i][1] ) % 2;
con_b[2] = ( con_a[2] + boat[i][2] ) % 2;
con_b[3] = ( con_a[3] + boat[i][3] ) % 2;
}
public static boolean isSafe(int[] con_b){
for(int i = 0; i < safe.length; i++ ) {
if ( Arrays.equals(con_b, safe[i]) ) {
return true;
}
}
return false;
}
public static void print(){
if ( con_b[0] == 1 ){
System.out.print("从A岸到B岸:");
}else{
System.out.print("从B岸到A岸:");
}
if( con_b[0] != con_a[0] ){
System.out.print("人");
}
if( con_b[1] != con_a[1] ){
System.out.print("带狼");
}
if( con_b[2] != con_a[2] ){
System.out.print("带羊");
}
if( con_b[3] != con_a[3] ){
System.out.print("带白菜");
}
System.out.print("过河");
}
public static void change(){
for (int i = 0; i < 4; i++){
con_a[i] = con_b[i];
}
}
}
运行结果
人 狼 羊 菜
初始状态: 0 0 0 0
--------------开始---------------
第1轮坐船方案
坐船: 1 1 0 0
该轮方案失败,尝试下一轮方案
---------------------------------
第2轮坐船方案
坐船: 1 0 1 0
该轮方案成功!
从A岸到B岸:人带羊过河
---------------------------------
第3轮坐船方案
坐船: 1 0 0 1
该轮方案失败,尝试下一轮方案
---------------------------------
第4轮坐船方案
坐船: 1 0 0 0
该轮方案成功!
从B岸到A岸:人过河
---------------------------------
第5轮坐船方案
坐船: 1 1 0 0
该轮方案成功!
从A岸到B岸:人带狼过河
---------------------------------
第6轮坐船方案
坐船: 1 0 1 0
该轮方案成功!
从B岸到A岸:人带羊过河
---------------------------------
第7轮坐船方案
坐船: 1 0 0 1
该轮方案成功!
从A岸到B岸:人带白菜过河
---------------------------------
第8轮坐船方案
坐船: 1 0 0 0
该轮方案成功!
从B岸到A岸:人过河
---------------------------------
第9轮坐船方案
坐船: 1 1 0 0
该轮方案失败,尝试下一轮方案
---------------------------------
第10轮坐船方案
坐船: 1 0 1 0
该轮方案成功!
从A岸到B岸:人带羊过河
过河成功!