Traveling Salesman Problem (Codeforces 1713A)
Tags: Greedy, geometry

You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point $$$(x, y)$$$, you can:   go left, and move to $$$(x - 1, y)$$$, or  go right, and move to $$$(x + 1, y)$$$, or  go up, and move to $$$(x, y + 1)$$$, or  go down, and move to $$$(x, y - 1)$$$. There are $$$n$$$ boxes on this plane. The $$$i$$$-th box has coordinates $$$(x_i,y_i)$$$. It is guaranteed that the boxes are either on the $$$x$$$-axis or the $$$y$$$-axis. That is, either $$$x_i=0$$$ or $$$y_i=0$$$.You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point $$$(0,0)$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of boxes.The $$$i$$$-th line of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-100 \le x_i, y_i \le 100$$$) — the coordinate of the $$$i$$$-th box. It is guaranteed that either $$$x_i=0$$$ or $$$y_i=0$$$.Do note that the sum of $$$n$$$ over all test cases is not bounded.

Output: For each test case output a single integer — the minimum number of moves required.

Note: In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.  $$$$$$(0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0)$$$$$$ In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.  $$$$$$(0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0)$$$$$$ In the third test case, we can collect all boxes without making any moves.

Sample Inputs:
3
4
0 -2
1 0
-1 0
0 2
3
0 2
-3 0
0 -1
1
0 0

Sample Outputs:
12
12
0

Solution (Python):
def i():
    n = int(input())
    mx = [0, 0]
    my = [0, 0]
    for j in range(n):
        s = input().split()
        if s[0] == '0':
            s[1] = int(s[1])
            if s[1] > my[1]:
                my[1] = s[1]
            elif s[1] < my[0]:
                my[0] = s[1]
        else:
            s[0] = int(s[0])
            if s[0] > mx[1]:
                mx[1] = s[0]
            elif s[0] < mx[0]:
                mx[0] = s[0]
    s = 0
    for x in zip(mx, my):
        s += abs(x[0]) + abs(x[1])
    return str(s * 2)
t = int(input())
a = []
for _ in range(t):
    a.append(i())
print('\n'.join(a))

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    x = [0]
    y = [0]
    for _ in range(n):
        (a, b) = map(int, input().split())
        x.append(a)
        y.append(b)
    print((max(x) - min(x) + max(y) - min(y)) * 2)

Solution (Python):
for _ in range(int(input(''))):
    a = int(input(''))
    thx = [0]
    thy = [0]
    th_x = [0]
    th_y = [0]
    for e in range(a):
        (x, y) = map(int, input('').split())
        if x != 0:
            if x > 0:
                thx.append(x)
            else:
                th_x.append(x)
        elif y != 0 and y > 0:
            thy.append(y)
        elif y != 0 and y < 0:
            th_y.append(y)
        else:
            thy.append(y)
    print(max(thy) * 2 + max(thx) * 2 + abs(min(th_y) * 2) + abs(min(th_x) * 2))