51,397
社区成员




import java.util.HashSet;
import java.util.Set;
public class Solution4 {
int[] m = new int[] { 12, 8, 5 };
int[] t = new int[] { 6, 6, 0 };
Set<String> set = new HashSet<>();
public void test(int[] c, String path) {
String cStr = String.format("%s+%s+%s", c[0], c[1], c[2]);
if (set.contains(cStr)) {
return;
}
path = path + "=>" + cStr;
String tStr = String.format("%s+%s+%s", t[0], t[1], t[2]);
if (tStr.equals(cStr)) {
System.out.println(path);
return;
}
set.add(cStr);
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (i == j) {
continue;
}
if (c[j] == m[j]) {
continue;
}
int[] temp = new int[c.length];
System.arraycopy(c, 0, temp, 0, c.length);
int swap = Math.min(m[j] - temp[j], temp[i]);
temp[i] = temp[i] - swap;
temp[j] = temp[j] + swap;
test(temp, path);
}
}
}
public static void main(String[] args) {
int[] c = new int[] { 12, 0, 0 };
String path = "开始";
new Solution4().test(c, path);
}
}