Prison Break (Codeforces 1457A)
Tags: Math, brute force

There is a prison that can be represented as a rectangular matrix with $$$n$$$ rows and $$$m$$$ columns. Therefore, there are $$$n \cdot m$$$ prison cells. There are also $$$n \cdot m$$$ prisoners, one in each prison cell. Let's denote the cell in the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$.There's a secret tunnel in the cell $$$(r, c)$$$, that the prisoners will use to escape! However, to avoid the risk of getting caught, they will escape at night.Before the night, every prisoner is in his own cell. When night comes, they can start moving to adjacent cells. Formally, in one second, a prisoner located in cell $$$(i, j)$$$ can move to cells $$$( i - 1 , j )$$$ , $$$( i + 1 , j )$$$ , $$$( i , j - 1 )$$$ , or $$$( i , j + 1 )$$$, as long as the target cell is inside the prison. They can also choose to stay in cell $$$(i, j)$$$.The prisoners want to know the minimum number of seconds needed so that every prisoner can arrive to cell $$$( r , c )$$$ if they move optimally. Note that there can be any number of prisoners in the same cell at the same time. 

Input: The first line contains an integer $$$t$$$ $$$(1 \le t \le 10^4)$$$, the number of test cases.Each of the next $$$t$$$ lines contains four space-separated integers $$$n$$$, $$$m$$$, $$$r$$$, $$$c$$$ ($$$1 \le r \le n \le 10^9$$$, $$$1 \le c \le m \le 10^9$$$).

Output: Print $$$t$$$ lines, the answers for each test case.


Sample Inputs:
3
10 10 1 1
3 5 2 4
10 2 5 1

Sample Outputs:
18
4
6

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

def solve():
    (n, m, r, c) = map(int, input().split())
    return max(r - 1 + c - 1, n - r + c - 1, n - r + m - c, r - 1 + m - c)
for _ in range(int(input())):
    print(solve())

Solution (Python):
for s in [*open(0)][1:]:
    (n, m, r, c) = map(int, s.split())
    print(max(m - c, c - 1) + max(r - 1, n - r))

Solution (Python):
import sys
import os
from io import BytesIO, IOBase

def some_random_function():
    """"""
    x = 10
    x *= 100
    i_dont_know = x
    why_am_i_writing_this = x * x
    print(i_dont_know)
    print(why_am_i_writing_this)

def some_random_function5():
    """"""
    x = 10
    x *= 100
    i_dont_know = x
    why_am_i_writing_this = x * x
    print(i_dont_know)
    print(why_am_i_writing_this)

def main():
    for _ in range(int(input())):
        (n, m, r, c) = map(int, input().split())
        d1 = r - 1 + (c - 1)
        d2 = n - r + (c - 1)
        d3 = n - r + (m - c)
        d4 = r - 1 + (m - c)
        ans = max(d1, max(d2, max(d3, d4)))
        print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
    return sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()