每日作业20200506 - 农夫过河

题目

农夫带着狼,, 白菜过河
	如果农夫没有在旁边看着它们, 狼就会吃羊, 羊会吃白菜
	但是小船一次只能载一人一物
	请用程序打印出农夫带它们过河的流程	

分析

    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 };   //表示过河前状态,0表示在a岸,1表示在b岸,初始都在a岸
    static int[][] boat = { {1,1,0,0}, {1,0,1,0}, {1,0,0,1}, {1,0,0,0} };  //所有过河方式,1表示坐船
    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 ){  //{1,1,1,1}为全到b岸,只要四者没有全部到b岸就一直过河
            for (int i = 0; i < 4; i++){      //对四种过河方式进行遍历循环
                System.out.println("第" + count++ + "轮坐船方案");

                /*System.out.print("a岸情况:" + "\t");
                array( con_a );*/
                System.out.print("坐船:" + "\t\t");
                array( boat[i] );

                move(i, con_a, boat);    //调用move()方法返回过完河后两岸状态

                /*System.out.print("b岸情况:" + "\t");
                array( con_b );*/

                if ( isSafe(con_b) ){   //调用isSafe()方法判断过完河后是否都处于安全状态
                    System.out.println("该轮方案成功!");
                    print();        //如果都安全则调用print()方法对该次过河进行打印
                    change();       //调用change()方法将过完河后的状态赋值给过河前状态
                    System.out.println();   //换行

                    /*System.out.print("此时a岸为:" + "\t");
                    array( con_a );*/
                }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 ){
        //System.out.println();
        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]) ) {   //传入的数组与isSafe数组相同,即安全
                return true;
            }
        }
        return false;
    }
    //打印的办法
    public static void print(){    //编写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岸:人带羊过河
过河成功!
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值