思路
两个正方形R1和R2组成一个大的正方形R3,据我观察,R1+R2组合起来的宽高不会大于理论上R3的宽高。
理论的R3是R1和R2能组成最大的R3,并且是对脚重合的状态,解释来说就是R1和R2的两个对角刚好重合的状态,不算相交。
真实的R3就是题目输入的值组成的R3.
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
static int g() {
return scan.nextInt();
}
public static void main(String[] args) {
int x1 = g(), y1 = g();
int x2 = g(), y2 = g();
int x3 = g(), y3 = g();
int x4 = g(), y4 = g();
long R1 = ((long)Math.abs(x1 - x2)) * ((long)Math.abs(y1 - y2));
long R2 = ((long)Math.abs(x3 - x4)) * ((long)Math.abs(y3 - y4));
// 计算真实的R3四个点
int maxY = Math.max(y1, Math.max(y2, Math.max(y3, y4))),
minY = Math.min(y1, Math.min(y2, Math.min(y3, y4)));
int maxX = Math.max(x1, Math.max(x2, Math.max(x3, x4))),
minX = Math.min(x1, Math.min(x2, Math.min(x3, x4)));
int realY = maxY - minY, theoryY = Math.abs(y1 - y2) + Math.abs(y3 - y4);
int realX = maxX - minX, theoryX = Math.abs(x1 - x2) + Math.abs(x3 - x4);
if (realY - theoryY < 0 && realX - theoryX < 0) {
long xj = (theoryY - realY) * (theoryX - realX);
System.out.println(R1 + R2 - xj);
} else {
System.out.println(R1 + R2);
}
scan.close();
}
}