黑马程序员技术交流社区

标题: 农夫过河 [打印本页]

作者: 自然╰→而然    时间: 2015-4-21 20:35
标题: 农夫过河
一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题。

作者: 我想努力半年    时间: 2015-4-21 20:35
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();
                }   
        }
}


作者: 依秋无泪    时间: 2015-4-21 20:53

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;
         
}
  
}
作者: yangengjie    时间: 2015-4-21 22:08
太难了吧,还没学到集合呢
作者: fanxia_2007    时间: 2015-4-21 22:47
这个问题有点难,终于有人给解答了。
作者: 自然╰→而然    时间: 2015-4-21 23:36
依秋无泪 发表于 2015-4-21 20:53
public class Test {
        /**
        一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当 ...

你这个数组弄的。。不是很满意啊
作者: 自然╰→而然    时间: 2015-4-21 23:37
我想努力半年 发表于 2015-4-21 20:43
1,人和猫过河
2,人回来再将鱼或者狗带到对岸
3,人和猫回来,再将狗或者鱼带到对岸

这个可以,谢谢了!
作者: luoriver    时间: 2015-5-2 21:28
学习下!
作者: dcw2015    时间: 2015-6-21 11:58
先带猫,接着带鱼;回来的时候把猫也带上;然后把猫留下 带狗过去;最后回去把猫带过来
作者: NOODLES    时间: 2015-8-26 01:03
学习了!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2