Jug 倒水问题 ZJU ACM
2010-07-26 11:03
330 查看
以前做过一个倒油的问题,和这道很像。
只是这里倒水可以往外倒,也可以往里接。假定外部可以倒出无数,也可以倒进无数。
最近比较忙,还没来得及格式化好输入输出。但算法就是下面这个了。
问题的链接
只是这里倒水可以往外倒,也可以往里接。假定外部可以倒出无数,也可以倒进无数。
最近比较忙,还没来得及格式化好输入输出。但算法就是下面这个了。
问题的链接
import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { int[] input = { 3, 5, 4 }; Jug j = new Jug(input); j.RunJug(); } } class Jug { private int[] data; private ArrayList<JugState> StateList = new ArrayList<JugState>(); public Jug(int[] input) { this.data = input; } public void RunJug() { JugState state = new JugState(); state.waterInA = 0; state.waterInB = 0; this.StateList.add(state); FindNext(state); } private void FindNext(JugState state) { if (state.waterInA == data[2] || state.waterInB == data[2]) { this.PrintStateList(); System.out.println("success"); } else { if (CanPourOut(state)) { PourOut(state); } if (CanPourIn(state)) { PourIn(state); } // Pour A to B if (state.waterInB < data[1] && state.waterInA > 0) { PourAToB(state); } // Pour B to A if (state.waterInB > 0 && state.waterInA < data[0]) { PourBToA(state); } } } private boolean ContainState(JugState state) { for(int i=0;i<this.StateList.size();i++) { if(StateList.get(i).waterInA==state.waterInA&&StateList.get(i).waterInB==state.waterInB) return true; } return false; } private void PourBToA(JugState state) { JugState nextState = new JugState(); if (state.waterInB > data[0] - state.waterInA) { nextState.waterInA = data[0]; nextState.waterInB = state.waterInB - (data[0] - state.waterInA); } else { nextState.waterInB = 0; nextState.waterInA = state.waterInA + state.waterInB; } if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } private void PourAToB(JugState state) { JugState nextState = new JugState(); if (state.waterInA > data[1] - state.waterInB) { nextState.waterInB = data[1]; nextState.waterInA = state.waterInA - (data[1] - state.waterInB); } else { nextState.waterInA = 0; nextState.waterInB = state.waterInB + state.waterInA; } if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } private void PourIn(JugState state) { JugState nextState = new JugState(); if (state.waterInA < data[0]) { nextState.waterInA = data[0]; nextState.waterInB = state.waterInB; if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } if (state.waterInB < data[1]) { nextState.waterInA = state.waterInA; nextState.waterInB = data[1]; if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } } private boolean CanPourIn(JugState state) { if (state.waterInA < data[0] || state.waterInB < data[1]) return true; return false; } private void PourOut(JugState state) { JugState nextState = new JugState(); if (state.waterInA > 0) { nextState.waterInA = 0; nextState.waterInB = state.waterInB; if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } if (state.waterInB > 0) { nextState.waterInA = state.waterInA; nextState.waterInB = 0; if (!ContainState(nextState)) { this.StateList.add(nextState); FindNext(nextState); this.StateList.remove(nextState); } } } private boolean CanPourOut(JugState state) { if (state.waterInA > 0 || state.waterInB > 0) return true; return false; } public void PrintStateList() { for(int i=0;i<this.StateList.size();i++) { System.out.println(this.StateList.get(i).waterInA+" , "+this.StateList.get(i).waterInB); } } public void PrintArray() { for (int i = 0; i < this.data.length; i++) { System.out.print(this.data[i]); } } } class JugState { int waterInA = 0; int waterInB = 0; }
相关文章推荐
- leetcode 365. Water and Jug Problem 两个水杯倒水问题 + 最大公约数Gcd辗转相除法
- ACM之隐式图的遍历---杯子倒水问题
- ACM之隐式图的遍历---杯子倒水问题
- 倒水问题
- 首做acm问题
- acm 使用穷举法解决人鸡狗米过河的问题
- acmcoder1001:Sum Problem细节问题
- [转]ACM 取石子问题
- 贪心——区间问题 hzu.acmclub.com 1126看电视
- ACM;邮局问题;动态规划;O(n3); 四边形法则可以优化,暂时没研究,等待添加;
- acm应该注意的数据类型问题
- 经典问题----倒水(详细解析)
- [算法][庞果网]倒水问题/量水问题
- 山东理工ACM【1135】C/C++经典程序训练5---图形打印问题
- 小白学ACM-括号配对问题(栈实现)
- 广度优先搜索——水杯倒水问题
- 南阳理工OJ:倒水问题
- 背包问题小总结 习题(动态规划01背包(第k优解)完全背包,多重背包)acm杭电HDU2639,HDU2602,HDU1114,HDU2191
- ACM double精度问题
- java解决 acm题中的读取整数问题