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

    你可能感兴趣的文章
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>