Two Tables (Codeforces 1555B)
Tags: Others, brute force

You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).You can't rotate any of the tables, but you can move the first table inside the room.   Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one?

Input: The first line contains the single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of the test cases.The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 \le W, H \le 10^8$$$) — the width and the height of the room.The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 \le x_1 < x_2 \le W$$$; $$$0 \le y_1 < y_2 \le H$$$) — the coordinates of the corners of the first table.The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 \le w \le W$$$; $$$1 \le h \le H$$$) — the width and the height of the second table.

Output: For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table.Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

Note: The configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$.In the second test case, there is no way to fit both tables in the room without intersecting.In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$.

Sample Inputs:
5
8 5
2 1 7 4
4 2
5 4
2 2 5 4
3 3
1 8
0 3 1 6
1 5
8 1
3 0 6 1
5 1
8 10
4 5 7 8
8 5

Sample Outputs:
1.000000000
-1
2.000000000
2.000000000
0.000000000

Solution (Python):
class Rect:

    def __init__(self, coordinates):
        (self.x1, self.y1, self.x2, self.y2) = map(int, coordinates)
        self.width = self.x2 - self.x1
        self.height = self.y2 - self.y1

    def area(self):
        return self.width * self.height

class Table:

    def __init__(self, dimensions):
        (self.width, self.height) = map(int, dimensions)

def space(c1, c2, c3, c4, length):
    emp_1 = c2 - c1
    emp_2 = c4 - c3
    if length <= emp_1 or length <= emp_2:
        return 0
    else:
        return abs(max(c2 - c1, c4 - c3) - length)
test = int(input())
solutions = []
for _ in list(range(test)):
    room = Table(input().split())
    t_1 = Rect(input().split())
    t_2 = Table(input().split())
    if t_1.width + t_2.width > room.width and t_1.height + t_2.height > room.height:
        solutions.append('-1')
    elif t_1.width + t_2.width > room.width:
        solutions.append(space(0, t_1.y1, t_1.y2, room.height, t_2.height))
    elif t_1.height + t_2.height > room.height:
        solutions.append(space(0, t_1.x1, t_1.x2, room.width, t_2.width))
    else:
        x_axis = space(0, t_1.x1, t_1.x2, room.width, t_2.width)
        y_axis = space(0, t_1.y1, t_1.y2, room.height, t_2.height)
        solutions.append(min(x_axis, y_axis))
for solution in solutions:
    print(solution)

Solution (Python):
import sys
import math
input = sys.stdin.readline
test = input()

def cal():
    (W, H) = map(int, input().split())
    (x1, y1, x2, y2) = map(int, input().split())
    (w, h) = map(int, input().split())
    minW = min(W - x2, x1)
    maxW = max(W - x2, x1)
    minH = min(H - y2, y1)
    maxH = max(H - y2, y1)
    if maxW + minW < w and maxH + minH < h:
        ans = -1
    elif maxW + minW >= w and maxH + minH < h:
        ans = w - maxW
        if ans < 0:
            ans = 0
    elif maxW + minW < w and maxH + minH >= h:
        ans = h - maxH
        if ans < 0:
            ans = 0
    elif maxW + minW >= w and maxH + minH >= h:
        ans = min(h - maxH, w - maxW)
        if ans < 0:
            ans = 0
    print(ans)
for j in range(int(test)):
    cal()

Solution (Python):
def read():
    (W, H) = map(int, input().split())
    (x1, y1, x2, y2) = map(int, input().split())
    (w, h) = map(int, input().split())
    return (W, H, x1, y1, x2, y2, w, h)

def solve(W, H, x1, y1, x2, y2, w, h):
    ans = 9999999999999
    if x2 - x1 + w <= W:
        ans = min(ans, max(0, w - x1), max(0, x2 - (W - w)))
    if y2 - y1 + h <= H:
        ans = min(ans, max(0, h - y1), max(0, y2 - (H - h)))
    if ans == 9999999999999:
        return -1
    else:
        return ans
reapeat = int(input())
end = ''
for i in range(reapeat):
    (W, H, x1, y1, x2, y2, w, h) = read()
    end += f'{solve(W, H, x1, y1, x2, y2, w, h)}\n'
print(end)