博客
关于我
编写程序,打印1到100之内的整数,但数字中包含7的要跳过,例如:17、27、71、72
阅读量:144 次
发布时间:2019-02-26

本文共 2467 字,大约阅读时间需要 8 分钟。

老农过河问题是一个经典的算法题目,要求通过最少的过河次数,使猫、狗、鱼安全地到达对岸。以下是解决方案的详细分析和代码实现。

问题分析

老农需要每次只带一只动物过河。当老农不在对岸时,必须确保对岸的动物之间没有冲突。如果对岸存在猫和狗,或者猫和鱼,就会发生不和谐的情况。因此,老农必须在过河前确保对岸的和谐状态。

解决思路

  • 状态判断:分为两种状态,老农在左岸或右岸。
  • 过河逻辑
    • 左岸到右岸:老农在左岸时,必须带动物过河,但必须保证对岸的和谐。若对岸和谐,则可以带动物过河;否则,带动物过河可能导致对岸不和谐,需放弃。
    • 右岸到左岸:老农在右岸时,必须先返回左岸。返回时,如果对岸和谐,则直接返回;否则,需检查对岸动物的顺序,选择可以安全带回的动物。
  • 和谐检查:使用一个方法判断对岸是否和谐,避免猫狗和猫鱼同时存在。
  • 动物顺序:使用队列来管理动物的进入和离开顺序,确保老农总是带最早进入对岸的动物过河。
  • 代码实现

    package com.itheima;import java.util.LinkedList;public class River {    LinkedList
    here = new LinkedList<>(); LinkedList
    there = new LinkedList<>(); String farmer = "老农"; String cat = "猫"; String fish = "鱼"; String dog = "狗"; int count = 0; public void river() { here.add(farmer); here.add(cat); here.add(fish); here.add(dog); while (!here.isEmpty()) { if (here.contains(farmer)) { hereRiver(); } else { thereRiver(); } } System.out.println("在河的右岸有" + there); } private void hereRiver() { here.remove(farmer); String animal = null; while ((animal = here.remove()) != null) { if (isHarmony(here)) { there.add(farmer); there.add(animal); System.out.println("第" + ++count + "次: 老农带着(" + animal + ")划船过河的右岸"); return; } else { here.add(animal); } } } private void thereRiver() { there.remove(farmer); String animal = null; if (isHarmony(there)) { System.out.println("第" + ++count + "次: 老农[什么都不带]划船回到河的左岸"); here.add(farmer); } else { while ((animal = there.remove()) != null) { if (isHarmony(there)) { here.add(farmer); here.add(animal); System.out.println("第" + ++count + "次: 老农带着(" + animal + ")划船回到河的左岸"); return; } else { there.add(animal); } } } } private boolean isHarmony(LinkedList
    list) { return !(list.contains(dog) && list.contains(cat)) && !(list.contains(cat) && list.contains(fish)); }}

    代码解释

    • river():初始化左岸和右岸的队列,开始过河循环。
    • hereRiver():老农在左岸时的过河方法,检查对岸和谐状态,决定是否带动物过河。
    • thereRiver():老农在右岸时的过河方法,先返回左岸,检查对岸和谐状态,决定是否带动物回去。
    • isHarmony():检查对岸是否和谐,返回布尔值,用于过河决策。

    通过这种方法,老农可以在最少的过河次数内安全地将所有动物带到对岸,确保对岸的和谐与安全。

    转载地址:http://wmgy.baihongyu.com/

    你可能感兴趣的文章
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    Orleans框架------基于Actor模型生成分布式Id
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.environ 没有设置环境变量
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
    查看>>
    OSCACHE介绍
    查看>>
    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
    查看>>
    OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
    查看>>
    SQL--mysql索引
    查看>>