博客
关于我
编写程序,打印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/

    你可能感兴趣的文章
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>