A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangruijing 中级黑马   /  2015-3-5 17:08  /  1417 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test10 {
        /**
         * 第10题: 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,
         * 狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题
         * @param args
         */
            //创建集合,表示出发点
              ArrayList<String> left=new ArrayList<String>();
        //创建集合,表示目的地
        ArrayList<String> right=new ArrayList<String>();
        //构造函数,向startP中添加元素
        public Test10(int methodNum)
        {       
                left.add("cat");
            left.add("dog");
            left.add("fish");
                if(methodNum!=1){
                        left.add("person");
                }
               
        }
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                new Test10(1).sToEnd();
                //new Test10(2).startCross();
               
        }
        //定义函数,用于判断某一边是否和谐
        public boolean safe1(ArrayList<String> al)
        {
                if(al.contains("cat") && al.contains("dog") || al.contains("cat") && al.contains("fish"))
                        return false;
                return true;
        }
        //判断某一边是否和谐
                        public boolean safe2(List<String> list)
                        {
                                //如果某一边没有人,猫单独和其他动物在一起,则不和谐
                                if(list.size() > 1 && list.contains("cat") && !list.contains("person"))
                                {
                                        return false;
                                }
                               
                                return true;
                        }
        //定义函数,将三者安全送往目的地
        public void sToEnd()
        {
                String str;
                for(int i=0;i<left.size();i++)
                {
                        str=left.get(i);
                        left.remove(str);
                        if(safe1(left))
                        {
                                System.out.println("老农带"+str+"过河");
                                toEnd(str);
                                break;
                            
                        }
                               
                }
        }
        //定义函数,保证目的地安全
        public void toEnd(String al)
        {
                //将由原点带过河的动物,放到河对岸
                        right.add(al);
                        //判断是否全部带过河
                        if(left.isEmpty())
                                System.out.println("农夫将所有动物安全带到河对岸!");
                        else
                        {
                                //判断目的地是否安全,否则选择合适动物带回到原点
                                if(safe1(right))
                                {
                                        System.out.println("老农单独返回");
                                        sToEnd();
                                }
                              
                                else
                                {
                                    String str;
                                    for(int i=0;i<right.size();i++)
                                        {
                                                str=right.get(i);
                                                right.remove(str);
                                                if(safe1(right))
                                                {
                                                        System.out.println("老农带"+str+"回到原点");
                                                        left.add(str);
                                                        sToEnd();
                                                        break;
                                                    
                                                }
                                               
                                    }
                       }
                        }       
        }
                public void startCross()
                {
                        while(left.size() > 1)
                        {
                                left.remove("person");
                                Random random = new Random(); //老农随机带走一只动物
                                int index = random.nextInt(left.size());
                                if(index == left.size() - 1 && left.size() > 1) //为防止老农带走刚带回来的动物
                                {
                                        left.add("person");
                                        continue;
                                }
                                String s = left.get(index);
                                left.remove(s);
                                if(safe2(left)) //如果老农带走该动物后,正岸没有冲突,则带走该动物
                                {
                                        right.add("person"); // 该动物和老农到达对岸
                                        right.add(s);
                                        System.out.println("老农带" + s + "到对岸");
                                       
                                        if(right.size() == 4) //当老农和动物全部到达对岸,则渡河完成
                                        {
                                                break;
                                        }
                                       
                                        right.remove("person"); //老农准备返回
                                        if(safe2(right)) //如果老农自己返回,对岸不会发生冲突,则老农自己返回
                                        {
                                                left.add("person"); //老农返回正岸
                                                System.out.println("老农单独返回");
                                        }
                                        else //否则,老农带着一只动物一起返回
                                        {
                                                String s2 = right.get(random.nextInt(right.size()));
                                                while(s2.equals(s)) //如果老农要带走的动物是刚带来的那只,则换一只带走
                                                {
                                                        s2 = right.get(random.nextInt(right.size()));
                                                }
                                                right.remove(s2);
                                                left.add(s2); //老农和一只动物返回正岸
                                                left.add("person");
                                                System.out.println("老农带着" + s2 + "返回原点");
                                        }
                                }
                                else //如果老农带走一只动物后,正岸发生冲突,则放下该动物
                                {
                                        left.add(s);
                                        left.add("person");
                                }
                        }
                        System.out.println("老农和所有的动物都成功带到河对岸");
                }

}


评分

参与人数 1技术分 +1 收起 理由
万合天宜 + 1 赞一个!

查看全部评分

12 个回复

倒序浏览
一道很有趣的题
回复 使用道具 举报
怎么感觉不用这么复杂···
回复 使用道具 举报
18234133910 发表于 2015-3-6 13:19
怎么感觉不用这么复杂···

这个程序用了两种方法,如果有简单的方法,希望能分享一下
回复 使用道具 举报
public class Test10 {
        public static void main(String[] args) {
                List<String> left = new ArrayList<String>();
                List<String> right = new ArrayList<String>();
                left.add( "猫" );
                left.add( "狗" );
                left.add( "鱼" );
               
                int useBoat = 0;
                String animal = null;
               
                while( !left.isEmpty() ){
                    useBoat++;
                        animal = null;
                        if( 1 == useBoat % 2 ){
                                for( int i = 0; i < left.size(); i++ ){
                                        animal = left.remove( i );
                                        if( lossAnimal(left) ){
                                                left.add( animal );
                                                continue;
                                        }else{
                                                break;
                                        }
                                }
                                System.out.println( "第" + useBoat + "次, 老农用船将" + animal + "运到河右岸" );
                                right.add( animal );
                        }else {                        
                                while( lossAnimal(right) ){
                                        for( int i = 0; i < right.size(); i++ ){
                                                 animal = right.remove( i );
                                                 if( lossAnimal(right) ){
                                                         right.add( animal );
                                                         continue;
                                                 }else{
                                                         break;
                                                 }
                                   }
                                }
                           if( animal == null ){
                                   System.out.println( "第" + useBoat + "次, 老农用船回到河左岸" );
                           }else{
                               System.out.println( "第" + useBoat + "次, 老农用船将" + animal + "运回河左岸" );
                               left.add( animal );
                           }
                        }
                }
        }
        
        public static boolean lossAnimal( List<String> list ){
                if( list.contains("猫") && list.contains("狗") ){
                        return true;
                }else if( list.contains("猫") && list.contains("鱼") ){
                        return true;
                }else{
                        return false;
                }                        
        }
}

评分

参与人数 1技术分 +1 收起 理由
万合天宜 + 1 很给力!

查看全部评分

回复 使用道具 举报
不错的题目
回复 使用道具 举报
谢谢分享,这道题我也做过!
回复 使用道具 举报
谢谢分享,这题我也看过···
回复 使用道具 举报
打个 这个对于我来说 有点高端了。。。。我还是看基础的   循环去
回复 使用道具 举报
有点意思啊,怎么感觉和汉诺塔似的?
回复 使用道具 举报
很有趣的题目
回复 使用道具 举报
比较复杂,之前就卡在这个题了整了小半天也没出来
回复 使用道具 举报
赞个   加油  ,  看有没有简化一点的代码、、、、、、、、、、、
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马