Create a program for water sort puzzle mix up.
import java.util.ArrayList;
public class Water implements Puzzle<ArrayList<Integer>> {
private int C;
private ArrayList<Integer> J;
public ArrayList<Integer> getStart() {
ArrayList<Integer> Begin = new ArrayList<Integer>();
for (int x = 0; x < J.size(); x ++){ Begin.add(0);}
return Begin;
}
public boolean findResult(ArrayList<Integer> config) {
for (Integer x : config){
if (x.compareTo(C) == 0){
return true;
}
}
return false;
}
public ArrayList<ArrayList<Integer>> pour (ArrayList<Integer> config){
ArrayList<ArrayList<Integer>> pourConfigs = new ArrayList<ArrayList<Integer>>();
for (int x = 0; x < J.size(); x++){
for (int y = 0; y < J.size(); y++){
ArrayList<Integer> CurrConf = new ArrayList<Integer>();
CurrConf.addAll(config);
if (x != y){
int GreatCapacity = J.get(y);
int CurrCapa = GreatCapacity - CurrConf.get(y) ;
if (CurrConf.get(x) <= CurrCapa){
CurrConf.set(x, CurrConf.get(y) + CurrConf.get(x) );
CurrConf.set(x, 0);
}
else {
CurrConf.set(y, GreatCapacity);
CurrConf.set(x, CurrConf.get(x) - CurrCapa);
}
pourConfigs.add(CurrConf);
}
}
}
return pourConfigs;
}
public ArrayList<ArrayList<Integer>> fill (ArrayList<Integer> config){
ArrayList<ArrayList<Integer>> fillConfigs = new ArrayList<ArrayList<Integer>>();
for(int x = 0; x < config.size(); x++){
ArrayList<Integer> CurrConf = new ArrayList<Integer>();
CurrConf.addAll(config);
CurrConf.set(x, J.get(x));
fillConfigs.add(CurrConf);
}
return fillConfigs;
}
public ArrayList<ArrayList<Integer>> dump (ArrayList<Integer> config){
ArrayList<ArrayList<Integer>> dumpConfigs = new ArrayList<ArrayList<Integer>>();
for(int x = 0; x < config.size(); x++){
ArrayList<Integer> CurrConf = new ArrayList<Integer>();
CurrConf.addAll(config);
CurrConf.set(x, 0);
dumpConfigs.add(CurrConf);
}
return dumpConfigs;
}
public ArrayList<ArrayList<Integer>> getNearest(ArrayList<Integer> config) {
ArrayList<ArrayList<Integer>> Nearest = new ArrayList<ArrayList<Integer>>();
Nearest.addAll(pour(config));
Nearest.addAll(fill(config));
Nearest.addAll(dump(config));
return Nearest;
}
public Water(int C, ArrayList<Integer> J){
this.C = C;
this.J = J;
}
public static void main(String[] args) {
if (args.length < 2){
System.out.println("Usage: java Water amount J1 J2 ... ");
System.exit(0);
}
Integer c = Integer.parseInt(args[0]);
ArrayList<Integer> y = new ArrayList<Integer>();
for(int x = 1; x < args.length; x ++ ){
y.add(Integer.parseInt(args[x]));
}
Puzzle<ArrayList<Integer>> water1 = new Water(c , y);
Solver <ArrayList<Integer>> x = new Solver <ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> solution = x.solvePuzzle(water1);
if (solution == null){
System.out.println("No solution.");
}
else {
int x = 0;
while (!(solution.isEmpty())){
System.out.print("Step "+x+":");
for (int m = 0; m < solution.get(0).size(); m++){
System.out.print(" " +solution.get(0).get(m));
}
System.out.println("");
solution.remove(0);
x++;
}
}
}
}
Comments
Leave a comment