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

    你可能感兴趣的文章
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>