参考排名第一的代码,简单粗暴。
public static boolean check(int[][] a, int xl, int xr, int yl, int yr, int zl, int zr) {
int[] v = new int[a.length];
int minx = Integer.MAX_VALUE, maxx = Integer.MIN_VALUE;
int miny = Integer.MAX_VALUE, maxy = Integer.MIN_VALUE;
int minz = Integer.MAX_VALUE, maxz = Integer.MIN_VALUE;
for (int n = 0; n < a.length; n++) {
if (a[n][0] - a[n][3] >= xl && a[n][0] + a[n][3] <= xr &&
a[n][1] - a[n][3] >= yl && a[n][1] + a[n][3] <= yr &&
a[n][2] - a[n][3] >= zl && a[n][2] + a[n][3] <= zr
) {
v[n] = 1;
} else {
minx = Math.min(minx, a[n][0] - a[n][3]);
maxx = Math.max(maxx, a[n][0] + a[n][3]);
miny = Math.min(miny, a[n][1] - a[n][3]);
maxy = Math.max(maxy, a[n][1] + a[n][3]);
minz = Math.min(minz, a[n][2] - a[n][3]);
maxz = Math.max(maxz, a[n][2] + a[n][3]);
}
}
int edge = xr - xl;
if (maxx - minx > edge || maxy - miny > edge || maxz - minz > edge) {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt();
int[][] a = new int[N][4];
int minx = Integer.MAX_VALUE, maxx = Integer.MIN_VALUE;
int miny = Integer.MAX_VALUE, maxy = Integer.MIN_VALUE;
int minz = Integer.MAX_VALUE, maxz = Integer.MIN_VALUE;
for (int n = 0; n < N; n++) {
for (int i = 0; i < 4; i++) {
a[n][i] = in.nextInt();
}
minx = Math.min(minx, a[n][0] - a[n][3]);
maxx = Math.max(maxx, a[n][0] + a[n][3]);
miny = Math.min(miny, a[n][1] - a[n][3]);
maxy = Math.max(maxy, a[n][1] + a[n][3]);
minz = Math.min(minz, a[n][2] - a[n][3]);
maxz = Math.max(maxz, a[n][2] + a[n][3]);
}
HashSet<Integer> can = new HashSet<Integer>();
for (int n = 0; n < N; n++) {
can.add(a[n][0] + a[n][3] - minx);
can.add(maxx - a[n][0] + a[n][3]);
can.add(a[n][1] + a[n][3] - miny);
can.add(maxy - a[n][1] + a[n][3]);
can.add(a[n][2] + a[n][3] - minz);
can.add(maxz - a[n][2] + a[n][3]);
}
int re = Integer.MAX_VALUE;
for (int tp : can) {
if (check(a, minx, minx + tp, miny, miny + tp, minz, minz + tp) ||
check(a, minx, minx + tp, miny, miny + tp, maxz - tp, maxz) ||
check(a, minx, minx + tp, maxy - tp, maxy, minz, minz + tp) ||
check(a, minx, minx + tp, maxy - tp, maxy, maxz - tp, maxz) ||
check(a, maxx - tp, maxx, miny, miny + tp, minz, minz + tp) ||
check(a, maxx - tp, maxx, miny, miny + tp, maxz - tp, maxz) ||
check(a, maxx - tp, maxx, maxy - tp, maxy, minz, minz + tp) ||
check(a, maxx - tp, maxx, maxy - tp, maxy, maxz - tp, maxz)
) {
if (tp < re) {
re = tp;
}
}
}
System.out.println("Case #" + (t + 1) + ": " + re);
}
}