商人过河 java_商人过河问题(二)java实现
packagemechants_River;importjava.util.ArrayList;importjava.util.List;importjava.util.Set;importjava.util.TreeSet;public classPath {
Dual carryingSchema[];//小船可提供的载人方案
Dual initStatus; //初始状态
Dual endStatus; //终止状态
List path = new ArrayList(); //过河路径
Set iNode = new TreeSet(); //孤立结点
public Path(int merchant, int servant, intcarrying) {
initStatus= newDual(merchant, servant);
endStatus= new Dual(0, 0);
buildCarryingSchema(carrying);
findPath();if(path.isEmpty()) {
System.out.println("Can‘t solve the problem");
}else{for(Node e : path) {
System.out.println(e);
}
}
}public static voidmain(String[] args) {
Path p= new Path(5, 5, 2);
}public booleanisRepeat() {return path.contains(this);
}/*** 构建渡河方案 根据小船的最大可载人数且小船不能空 计算可行方案 每个方案表示一个dual 对象 并把保存在carryingSchema中。
* 在Node中,通过数组下标来引用小船的载人方案。 数组既可以正向遍历,也可以反向遍历,所有的载人方案,按总人数进行降序排列
*
*@paramcarrying*/
public void buildCarryingSchema(intcarrying) {int size = (2 + carrying + 1) * carrying / 2; //方案总数
carryingSchema = newDual[size];//小船载人方案;按人数降序排列
for (int i = 0, total = carrying; total > 0; total--) {for (int m = total; m >= 0; m--) {
carryingSchema[i++] = new Dual(m, total -m);
}
}
}/*** 使用穷举法,搜索一条即初始状态initStatus至终止状态endStatus的迁移路径 step表示渡河步骤,从0开始,step为偶数,表示前行
* 小船前行时,正向遍历表carrySchema
* 如果无法找到可行的方案,则当前结点为孤立结点,则从path中删除,置入孤立结点集合中,同时step回退一步
* ,继续搜索可行的渡河方案。当step<0时,则表示无法找到可行的渡河方案,path将为空。*/
public voidfindPath() {
Dual curStatus=initStatus;int step = 0;
Node node= null, rnode = null;while (step >= 0) {int direction = (step % 2 == 0 ?Node.FORWARD : Node.BACKWAED);intidx;if (direction ==Node.FORWARD) {
idx= 0;//mode不空,表示发送路径回退。需要跳过该结点已尝试的方案
if (rnode != null) {
idx= rnode.idx + 1;
rnode= null;
}
}else{//方向为backword
idx = carryingSchema.length - 1;//mode不空,表示发送路径回退.需要跳过结点
if (rnode != null) {
idx= rnode.idx - 1;
rnode= null;
}
}boolean found = false;while (!found && idx >= 0 && idx
node= newNode(curStatus, direction, idx);if (node.isValid() && //渡河方案是否有效
node.isSecure() && //状态是否安全
!node.isRepeat() && //结点不能重复
!node.isIsolated()) //非孤立结点
{
found= true;
}else{if (direction ==Node.FORWARD)
idx++; //顺序搜索
elseidx--; //逆序搜索
}
}if (!found) { //未找到可行的渡河方案
step--; //回退一步, 并删除当前结点
if (step >= 0) {
curStatus=path.get(step).status;
rnode=path.remove(step);
iNode.add(rnode);//孤立结点
}continue;
}
path.add(node);//把当前结点加入路径中
if(node.isEnd())break; //是否到达终止状态
curStatus = node.nextStatus(); //当前状态变迁
step++;
System.out
.println(step+ " " + node.status.m + " " +node.status.s);
}
}/*** status 节点方向
* direction 渡河方向
* idx 索引,指向小船的载人方案 carryingSchema[]
*@authortina
* Node 表示整个渡河方案的每个步骤,每个结点由结点状态(商人和随从的人数)、渡河方向和渡河方案组成*/
public class Node implements Comparable{
Dual status;intdirection;intidx;public static final int FORWARD = 0; //前进
public static final int BACKWAED = 1; //返回
public Node(Dual status, int direction, intidx) {this.status =status;this.direction =direction;this.idx =idx;
}
@Overridepublic intcompareTo(Node e) {return this.toString().compareTo(e.toString());
}
@OverridepublicString toString() {return "("+this.status.m+","+this.status.s+")" + (direction == FORWARD ? " ---> " : "
}public booleanisIsolated() {//int direction = this.nextDirection();
Dual status = this.nextStatus();for(Node e : iNode) {if (direction == e.direction &&status.equals(e.status)) {return true;
}
}return false;
}
Dual nextStatus() {return direction == FORWARD ? this.status
.minus(carryingSchema[idx]) :this.status
.add(carryingSchema[idx]);
}public booleanequals(Object e) {if (this ==e)return true;if (!(e instanceofNode))return false;
Node o=(Node) e;return this.status.equals(o.status)&& this.direction == o.direction && this.idx ==o.idx;
}/*** 当向前渡河时,要求本岸商人和随从人数分别大于等于渡河方案指定的商人和随从人数;
* 当小船返回时,要求对岸商人和随从人数分别大于等于渡河方案指定的商人和随从人数
*
*@return
*/
public booleanisValid() {return this.direction == FORWARD ?status
.greaterOrEqual(carryingSchema[idx]) : initStatus.minus(
status).greaterOrEqual(carryingSchema[idx]);
}/*** 判断结点的状态是否有效,这是实际问题的一具体约束,即要求结点状态中商人人数或者为0,或者不小于随从人数。
*@return
*/
public booleanisSecure() {
Dual next= this.nextStatus();return (next.m == 0 || next.m >=next.s)&& (initStatus.m - next.m == 0 || initStatus.m - next.m >=initStatus.s-next.s);
}public booleanisRepeat() {returniscontains(path);
}public boolean iscontains(Listp) {for (int i = 0; i < p.size(); i++) {
Node o=p.get(i);if (this.status.m ==o.status.m&& (this.status.s ==o.status.s)&& this.direction == o.direction && this.idx ==o.idx) {
System.out.println("equal");return true;
}
}if(p.size()>=1&&this.idx==p.get(p.size()-1).idx){return true;
}return false;
}public booleanisEnd() {
Dual next= this.nextStatus();return next.m==endStatus.m&&next.s==endStatus.s;
}
}
}
商人过河 java_商人过河问题(二)java实现相关推荐
- matlab三个商人三个随从,商人们怎样安全过河 (附MATLAB程序完整)
商人们怎样安全过河 随从们密约,在河的任一岸,一旦随从 的人数比商人多,就杀人越货. 但是乘船渡河的方案由商人决定. 商人们怎样才能安全过河? 问题分析:多步决策过程 决策~每一步(此岸到彼岸或彼岸到 ...
- matlab程序4名商人,商人们怎样安全过河附MATLAB程序完整.doc
商人们怎样安全过河附MATLAB程序完整.doc *** 商人们怎样安全过河 随从们密约, 在河的任一岸, 一旦随从的人数比商人多, 就杀人越货. 但是乘船渡河的方案由商人决定. 商人们怎样才能安全过 ...
- java农夫过河_农夫过河问题(java版)
packagecom.my.courseDesign;public classCourseDesign {/** 1. 首先分为A岸,和B岸,A岸用0来表示,B岸用1来表示,在船上用1来表示,不再船上 ...
- java数据结构 农夫过河,数据结构农夫过河
农夫过河问题(C++编写)_电子/电路_工程科技_专业资料.1.问题描述从前,一... 农夫过河的安全步骤: NO1:农夫,狼,羊,白菜都在河的左岸 NO2:农夫带羊到... 南阳理工学院 " ...
- 【Java_项目篇1】--JAVA实现坦克大战游戏--坦克移动+添加敌方坦克(二)
前期文章: [Java_项目篇<1>]--JAVA实现坦克大战游戏--画出坦克(一) 控制小球移动 1.外部类 实现KeyListener监听接口写法 package com.test3; ...
- flash静态的农夫走路_智力游戏过河|智力游戏过河flash合集下载 _单机游戏下载...
智力游戏过河,过河游戏是经典的益智游戏了,需要很高的IQ,能很好锻炼逻辑能力,经典的过河游戏有人鬼过河,农夫过河,高IQ过河,青蛙过河等等.跑跑车为您提供的智力游戏过河flash合集,包含了这一些游戏 ...
- java怎么连发子弹_【Java_项目篇1】--JAVA实现坦克大战游戏--子弹连发+爆炸效果(四)...
前期相关文章 [Java_项目篇<1>]–JAVA实现坦克大战游戏–画出坦克(一) [Java_项目篇<1>]–JAVA实现坦克大战游戏–坦克移动+添加敌方坦克(二) [Jav ...
- 20175333曹雅坤 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...
- java基本语句回文数实验_实验二 java基本数据类型与把持语句.doc
实验二 java基本数据类型与把持语句.doc 还剩 4页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,喜欢就下载吧,价低环保! 内容要点: 5System.out.println(num ...
最新文章
- 腾讯AI足球队夺冠Kaggle竞赛,绝悟AI强化学习框架通用性凸显
- iOS开发线程同步技术-锁
- 将BLOG作为输入平台 ……
- 自定义SpringBoot start 自动打印日志
- php7 匿名继承类_PHP7匿名类的用法示例
- Nginx文档阅读笔记-DNS load balancing(DNS负载均衡)
- inner join ,left join ,right join 以及java时间转换
- Visual C# 2005——超好用的DropDown与DropDownClosed事件
- 手机当启动U盘DriveDroid 版本 0.10.3 功能无限制版使用教程
- wifi mouse hd for linux,wifi mouse hd客户端PC版下载_wifi mouse hd客户端PC版官方下载-太平洋下载中心...
- Python爬虫 抓取拉勾招聘信息
- 自己写的Excel查询小工具,需要的可以拿走
- 《阵列信号处理及MATLAB实现》绪论、矩阵代数相关内容总结笔记
- Nginx反向代理的配置
- 如何在服务器上安装虚拟机呢?服务器虚拟机安装教程
- 中规中矩的输入两个正整数m和n,求其最大公约数和最小公倍数。
- 分享--操作系统学习
- mac下如何将adobe acrobat reader dc设为.pdf的默认打开方式?
- 海量智库第4期|Vastbase G100核心技术介绍之【NUMA架构性能优化技术】
- Windows Loader v2.1.1