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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 Kevin.Kang 于 2015-8-4 13:22 编辑
* 第十题:一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
* 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。
* 编程解决猫狗鱼过河问题。

本来想法是在网上随便找一个抄一下,找到的都不怎么满意,索性就按照自己的想法,结合网上找的代码,重新优化写出了一个比较简洁直观的程序。

  1. package com.itheima;
  2. /*
  3. * 第十题:一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
  4. * 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。
  5. * 编程解决猫狗鱼过河问题。
  6. */
  7. import java.util.ArrayList;

  8. public class Test10 {
  9.         // 创建一个集合,代表起点.
  10.         public static ArrayList<String> here = new ArrayList<String>();
  11.         // 创建一个集合,代表终点.
  12.         public static ArrayList<String> there = new ArrayList<String>();

  13.         public static void main(String[] args) {
  14.                 // 添加元素.
  15.                 here.add("Dog");
  16.                 here.add("cat");
  17.                 here.add("fish");

  18.                 take();
  19.         }

  20.         // 定义一个方法,用来判断这三个动物之间关系
  21.         public static boolean safe(ArrayList<String> al) {
  22.                 // 当猫狗,猫鱼在一起的时候是不安全的
  23.                 if (al.contains("dog") && al.contains("cat") || al.contains("cat")
  24.                                 && al.contains("fish")) {
  25.                         return false;
  26.                 }
  27.                 return true;
  28.         }

  29.         // 定义一个方法,将起点的动物送到终点
  30.         public static void take() {
  31.                 // 得到一个动物
  32.                 String s = here.get(0);
  33.                 // 在起点删除此动物
  34.                 here.remove(s);
  35.                 // 判断起点剩下动物关系是否安全
  36.                 if (safe(here)) {
  37.                         // 添加此动物到终点中
  38.                         there.add(s);
  39.                         System.out.println("带着" + s + "\t去往终点。起点有" + here + ",终点有" + there);
  40.                         // 判断终点动物关系是否安全
  41.                         thereSafe();
  42.                 } else {
  43.                         // 动物关系不安全就不带此动物,重新来一次。第一次只能带猫去对面
  44.                         here.add(s);
  45.                         take();
  46.                 }
  47.         }

  48.         // 判断终点的元素
  49.         public static void thereSafe() {
  50.                 // 如果起点没有动物存在,就结束语句。
  51.                 if (here.isEmpty()) {
  52.                         System.out.println(there + "都安全过河了");
  53.                         return;
  54.                 }

  55.                 // 终点安全就接着去起点带动物
  56.                 if (safe(there)) {
  57.                         take();
  58.                 } else {
  59.                         // 不安全就将终点原先动物带回起点
  60.                         String temp = there.get(0);
  61.                         there.remove(temp);
  62.                         here.add(temp);
  63.                         System.out.println("带着" + temp + "\t回到起点。起点有" + here + ",终点有"
  64.                                         + there);
  65.                         take();
  66.                 }
  67.         }
  68. }
复制代码



9 个回复

倒序浏览
楼主逻辑太强了,我等佩服

点评

其实很好想,就是代码实现有点麻烦  发表于 2015-8-4 17:26
回复 使用道具 举报
楼主又见你了   我是群里的
回复 使用道具 举报
不错,赞一个!
回复 使用道具 举报
我很久之前看到这题了 看的都是集合方法,这两天学到集合,还是没头绪啊,不过看看你的  。佩服。。
回复 使用道具 举报
楼主强,佩服
回复 使用道具 举报
风刃 初级黑马 2016-3-22 23:53:33
7#
好6,终于有点头绪了
回复 使用道具 举报
顶...顶....顶
回复 使用道具 举报
这个问题最好不用编程解决
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马