51,398
社区成员




public class Solver {
//每个桶当前水量
int[] cup = new int[3];
//每个桶 容量
int[] max = new int[3];
//上一步操作中没有动过的桶
int lastfree;
//上一步操作中满或者空的桶
int lastreach;
public Solver() {
max[0] = 13;
max[1] = 17;
max[2] = 30;
cup[2] = 30;
}
public void next() {
int from;
int to;
//如果上一步有桶空了,则应该从上一步没动过的桶倒到该桶
if (cup[lastreach] == 0) {
to = lastreach;
from = lastfree;
}else if(cup[lastreach] == max[lastreach]) {
//反之如果有桶满了,则应该从该桶倒到没动过的桶
from = lastreach;
to = lastfree;
}else {
//根据论证,每一步必有一个桶空或者满
throw new IllegalStateException("Something must be wrong");
}
if(cup[from] > max[to] - cup[to]) {
cup[from] -= max[to] - cup[to];
cup[to] = max[to];
lastreach = to;
}else {
cup[to] += cup[from];
cup[from] = 0;
lastreach = from;
}
lastfree = 3 - (from + to);
}
}
public class Solver {
//每个桶当前水量
int[] cup = new int[3];
//每个桶 容量
int[] max = new int[3];
//上一步操作中没有动过的桶
int lastfree;
//上一步操作中满或者空的桶
int lastreach;
public Solver() {
max[0] = 13;
max[1] = 17;
max[2] = 30;
cup[2] = 30;
}
public void next() {
int from;
int to;
//如果上一步有桶空了,则应该从上一步没动过的桶倒到该桶
if (cup[lastreach] == 0) {
to = lastreach;
from = lastfree;
}else if(cup[lastreach] == max[lastreach]) {
//反之如果有桶满了,则应该从该桶倒到没动过的桶
from = lastreach;
to = lastfree;
}else {
//根据论证,每一步必有一个桶空或者满
throw new IllegalStateException("Something must be wrong");
}
if(cup[from] > max[to] - cup[to]) {
cup[from] -= max[to] - cup[to];
cup[to] = max[to];
lastreach = to;
}else {
cup[to] += cup[from];
cup[from] = 0;
lastreach = from;
}
lastfree = 3 - (from + to);
}
}