程序解:现有3L容器和5L容器各一个,问如何量出4L水(水无限)

news/2025/2/13 23:04:52/

穷举所有的可能性,从杯子里面水变化角度来讲,每个状态到下一个状态只有6种行为:3L杯子装满,5L杯子装满,3L倒空,5L倒空,3L倒到5L,5L倒到3L。最终结果如果5L的里面有4L水则得到一个解。如果当前状态和前面一个状态重复,则取消继续往后的探索。


public class test{private static Status[] steps = new Status[100];public static void main(String[] args){steps[0] = new Status();solve(1);}private static void solve(final int depth){//paint(depth-1);if(duplicate(depth - 1)){return;}if(steps[depth - 1].cup5 == 4){paint(depth - 1);return;}Status preStatus = steps[depth - 1];steps[depth] = preStatus.full3();solve(depth + 1);steps[depth] = preStatus.full5();solve(depth + 1);steps[depth] = preStatus.trans3to5();solve(depth + 1);steps[depth] = preStatus.trans5to3();solve(depth + 1);steps[depth] = preStatus.empty3();solve(depth + 1);		steps[depth] = preStatus.empty5();solve(depth + 1);}private static boolean duplicate(int depth){for(int i=0 ; i < depth ; i++){if( steps[i].equals(steps[depth])){return true;}}return false;}private static void paint(int depth){for(int i = 0; i <= depth; i++){System.out.print(steps[i]+" ");}System.out.println();}}class Status{int cup3;int cup5;public Status(){}public Status(Status s){this.cup3 = s.cup3;this.cup5 = s.cup5;}public Status full3(){Status newStatus = new Status(this);newStatus.cup3 = 3;return newStatus;}public Status full5(){Status newStatus = new Status(this);newStatus.cup5 = 5;return newStatus;}public Status trans3to5(){Status newStatus = new Status(this);if(5 - newStatus.cup5 >= newStatus.cup3){		newStatus.cup5 += newStatus.cup3;newStatus.cup3 = 0;}else{newStatus.cup3 -= (5 - newStatus.cup5);newStatus.cup5 = 5;}return newStatus;		}public Status trans5to3(){Status newStatus = new Status(this);if(newStatus.cup5 <= 3 - newStatus.cup3){newStatus.cup3 += newStatus.cup5;		newStatus.cup5 = 0;}else{newStatus.cup5 -= (3 - newStatus.cup3);newStatus.cup3 = 3;}return newStatus;		}public Status empty3(){Status newStatus = new Status(this);newStatus.cup3 = 0;return newStatus;}public Status empty5(){Status newStatus = new Status(this);newStatus.cup5 = 0;return newStatus;}public String toString(){return "["+this.cup3+"#"+this.cup5+"]";}public boolean equals(Object o){if(o == null){return false;}if(o == this){return true;}if(o instanceof Status){Status s = (Status)o;return s.cup3 == this.cup3 && s.cup5 == this.cup5;}return false;}
}



http://www.ppmy.cn/news/573930.html

相关文章

python实现无刻度3升水和5升水准确得到4升水的代码步骤

瓶子灌水问题: 两个没有刻度的杯子&#xff0c;一个容积是3L,一个容积是5L,怎么计量出4L水&#xff1f; 进阶1: 两个没有刻度的杯子&#xff0c;一个容积是A,一个容积是B,最终可计量水的体积。 c 跟A, B 什么关系&#xff1f; A 3; B 5 C {1, 2, 3, 4, 5} A 2; B 4 C {2,…

逻辑思维面试题-河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水?

河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水? 解1 设&#xff1a; A为5L 。 B为6L。 解&#xff1a; &#xff08;1&#xff09;5L的装满,全倒向6L中&#xff1b;此时B中有5L水&#xff08;空1L&#xff09;. &#xff08;2&#xff09;5L的再装满,再倒向…

C语言再学习 -- 文件

文件是什么 一个文件&#xff08;file&#xff09;通常就是磁盘上的一段命名的存储区。C 将文件看成是连续的字节序列&#xff0c;其中每一个字节都可以单独地读取。 二进制和文本模式 1、在windows系统中&#xff0c;文本模式下&#xff0c;文件以"\r\n"代表换行。若…

L3-Day13

⏰打卡时间&#xff1a;9月12日&#xff08;周四&#xff09; 6:00-17:00训练技巧顺序&#xff1a;【完全听写法】️【车轮法】️【影子跟读法】⏱【练习时间】30 mins句1: But at one time, some businesses refused to employ people who were on a blacklist for belonging …

YoloV4学习笔记5_yolov4-tiny(win10+VC2015+opencv4.4+yolov4)

YoloV4学习笔记5_yolov4-tiny&#xff08;win10VC2015opencv4.4yolov4&#xff09; 对于cpu识别或者资源不太充足的设备&#xff0c;yolov4的完整版太慢了。 yolov4-tiny相当于yolov4的缩小版本&#xff0c;速度比完整版快10倍&#xff0c;当然也牺牲了一些准确度。 》YOLOv4-…

shell3

http://note.youdao.com/noteshare?idae8785c0aff0587e91a01fd504f10107&subCB3C2FAC835845F6BAA8CFBF4BBED2FF

3ll4c语言,3l英语

概括&#xff1a;这道题是王嗡行同学的课后数学练习题&#xff0c;主要是关于3l英语&#xff0c;指导老师为訾老师。此书可以说是《新概念英语的青少年版》。全套教材按听、说、读、写顺序安排&#xff0c;实际运用英语的技能训练重于英语知识的学习。这一点既是这套教材的特点…

1个5L的桶和一个3L的水桶怎么量出4L的水?所带来的思考

问题:1个5L的桶和一个3L的水桶怎么量出4L的水? 答案: 先把3L灌满 倒进5L的桶里 此时5L的桶里有3L水再把3L灌满 往5L的桶里倒 5L的满了以后 此时3L的桶里还剩1L把5L桶里的水倒掉 刚才3L桶里省下的1L倒进去再灌满3L的 再倒进去 就是4L了 思考: 起初,想这个问题用了很久…