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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 自然╰→而然 初级黑马   /  2015-4-21 20:35  /  3333 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

20黑马币
一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题。

最佳答案

查看完整内容

1,人和猫过河 2,人回来再将鱼或者狗带到对岸 3,人和猫回来,再将狗或者鱼带到对岸 4,人回来,将猫带到对岸 import java.util.ArrayList; public class Test10 { public static void main(String[] args) { new Test10().tohere(); } //创建一个集合,代表起始点. ArrayList here = new ArrayList(); //创建一个集合,代表终点. ArrayList there = new ArrayList(); //添加元素. pub ...

9 个回复

倒序浏览
1,人和猫过河
2,人回来再将鱼或者狗带到对岸
3,人和猫回来,再将狗或者鱼带到对岸
4,人回来,将猫带到对岸
import java.util.ArrayList;
public class Test10 {
       public static void main(String[] args)
{            
                        new Test10().tohere();
}      
        //创建一个集合,代表起始点.  
        ArrayList<String> here = new ArrayList<String>();
        //创建一个集合,代表终点.
         ArrayList<String> there = new ArrayList<String>();  
        //添加元素.     
         public Test10() {            
                here.add("Dog");         
                here.add("cat");         
                here.add("fish");   
        }         
        //定义一个方法,用来判断这三个动物之间关系.     
         public boolean isSafe(ArrayList<String> al)  {         
                 if (al.contains("dog") && al.contains("cat")|| al.contains("cat") && al.contains("fish"))
                 {                          
                        return false;   
                 }               
                return true;

         }         
       //定义一个方法,将起点的元素送到终点.         
        public void tohere() {      
                String str = here.get(0);               
                here.remove(str);           
                if (isSafe(here)) {        
                        System.out.println("1农夫带着" + str + "去往对岸,这边还剩下" + here + ",对岸有" +  there);//dog []                    
                        toThere(str);

                 } else {      
                        here.add(str);                    
                        tohere();     
                }   
        }  
        //定义一个方法,用来查看终点的元素      
         public void toThere(String s) {                     
                if (isSafe(there)) {           
                        there.add(s);

                        if(here.isEmpty()){              
                                System.out.println("4农夫,"+there+"都被你带过来了");
                                return;                 
                         }                             
                        if(isSafe(there)){      
                                 System.out.println("2农夫回到原点,对岸有" + there);
                                  tohere();
                        }
                        else{         
                                String temp=there.get(0);   
                                there.remove(temp);   
                                System.out.println("3农夫带着"+temp+"回到原点,这边有" + here + ",对岸有" + there);
                                                here.add(temp);                 
                                tohere();         
                        }                     
                }
                else {                           
                        there.remove(s);                           
                        tohere();
                }   
        }
}

回复 使用道具 举报

public class Test {
        /**
        一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题
         * @param args
         */
        static int []a={0,0,0};//代表过河的动物,分别代表狗猫鱼,0代表未过河,1代表过河
public static void main(String []args)
{
       
       
         for(int i=0;i<3;i++)
         {   
               
               
                 for(int j=0;j<2;j++)
                 {
                         a[0]=0;//狗
                         a[1]=0;//猫
                         a[2]=0;//鱼
                         a[i]=1;
                         
                         if(i==0) System.out.print("狗先过河  ");
                         if(i==1) System.out.print("猫先过河  ");
                         if(i==2) System.out.print("鱼先过河  ");
                         if(!pd()){System.out.print("过河失败! \n");break;}
                                 
                         if(j==0)
                         {  
                                 
                                 
                                 if(i==0) {System.out.print("猫过河  ");a[1]=1;}
                                 if(i==1) {System.out.print("鱼过河  ");a[2]=1;}
                                 if(i==2) {System.out.print("狗过河  ");a[0]=1;}
                                 if(!pd())System.out.print("过河失败!\n ");else
                                         {
                                         if(i==0) {System.out.print("鱼过河  ");}
                                         if(i==1) {System.out.print("狗过河  ");}
                                         if(i==2) {System.out.print("猫过河  ");}
                                         System.out.print("过河成功!\n");
                                         
                                         }
                                 
                         }
                         if(j==1)
                         {
                                 if(i==0) {System.out.print("鱼过河 ");a[2]=1;}
                                 if(i==1) {System.out.print("狗过河 ");a[0]=1;}
                                 if(i==2) {System.out.print("猫过河 ");a[1]=1;}
                                 if(!pd())System.out.println("过河失败!\n");else
                                 {
                                         if(i==0) {System.out.print("猫过河  ");}
                                         if(i==1) {System.out.print("鱼过河  ");}
                                         if(i==2) {System.out.print("狗过河  ");}
                                         System.out.print("过河成功! \n");}
                         }
                       
                 }
       
                 
         }
         
         
}
public static boolean pd()//过河的判断方法
{   /*假设已经过河的动物不会相互排斥*/
         if(a[0]==0&&a[1]==0){System.out.print("狗会咬猫 ");return false;}
         if(a[1]==0&&a[2]==0){System.out.print("猫会吃鱼 ");return false;}
        /*  假设已经过河的动物也会相互排斥
         if((a[0]==0&&a[1]==0)||(a[0]==1&&a[1]==1)){System.out.print("狗会咬猫 ");return false;}
         if((a[1]==0&&a[2]==0)||(a[1]==1&&a[2]==1)){System.out.print("猫会吃鱼 ");return false;}
         */
         return true;
         
}
  
}
回复 使用道具 举报
太难了吧,还没学到集合呢
回复 使用道具 举报
这个问题有点难,终于有人给解答了。
回复 使用道具 举报
依秋无泪 发表于 2015-4-21 20:53
public class Test {
        /**
        一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当 ...

你这个数组弄的。。不是很满意啊
回复 使用道具 举报
我想努力半年 发表于 2015-4-21 20:43
1,人和猫过河
2,人回来再将鱼或者狗带到对岸
3,人和猫回来,再将狗或者鱼带到对岸

这个可以,谢谢了!
回复 使用道具 举报
学习下!
回复 使用道具 举报
先带猫,接着带鱼;回来的时候把猫也带上;然后把猫留下 带狗过去;最后回去把猫带过来
回复 使用道具 举报
学习了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马