Robot Breakout (Codeforces 1196C)
Tags: Implement, implementation

$$$n$$$ robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the $$$i$$$-th robot is currently located at the point having coordinates ($$$x_i$$$, $$$y_i$$$). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers $$$X$$$ and $$$Y$$$, and when each robot receives this command, it starts moving towards the point having coordinates ($$$X$$$, $$$Y$$$). The robot stops its movement in two cases:  either it reaches ($$$X$$$, $$$Y$$$);  or it cannot get any closer to ($$$X$$$, $$$Y$$$). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as ($$$x_c$$$, $$$y_c$$$). Then the movement system allows it to move to any of the four adjacent points:  the first action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c - 1$$$, $$$y_c$$$);  the second action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c + 1$$$);  the third action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c + 1$$$, $$$y_c$$$);  the fourth action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c - 1$$$). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers $$$X$$$ and $$$Y$$$ so that each robot can reach the point ($$$X$$$, $$$Y$$$). Is it possible to find such a point?

Input: The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.Then $$$q$$$ queries follow. Each query begins with one line containing one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of robots in the query. Then $$$n$$$ lines follow, the $$$i$$$-th of these lines describes the $$$i$$$-th robot in the current query: it contains six integer numbers $$$x_i$$$, $$$y_i$$$, $$$f_{i, 1}$$$, $$$f_{i, 2}$$$, $$$f_{i, 3}$$$ and $$$f_{i, 4}$$$ ($$$-10^5 \le x_i, y_i \le 10^5$$$, $$$0 \le f_{i, j} \le 1$$$). The first two numbers describe the initial location of the $$$i$$$-th robot, and the following four numbers describe which actions the $$$i$$$-th robot can use to move ($$$f_{i, j} = 1$$$ if the $$$i$$$-th robot can use the $$$j$$$-th action, and $$$f_{i, j} = 0$$$ if it cannot use the $$$j$$$-th action).It is guaranteed that the total number of robots over all queries does not exceed $$$10^5$$$.

Output: You should answer each query independently, in the order these queries appear in the input.To answer a query, you should do one of the following:  if it is impossible to find a point that is reachable by all $$$n$$$ robots, print one number $$$0$$$ on a separate line;  if it is possible to find a point that is reachable by all $$$n$$$ robots, print three space-separated integers on the same line: $$$1$$$ $$$X$$$ $$$Y$$$, where $$$X$$$ and $$$Y$$$ are the coordinates of the point reachable by all $$$n$$$ robots. Both $$$X$$$ and $$$Y$$$ should not exceed $$$10^5$$$ by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding $$$10^5$$$ by absolute value.


Sample Inputs:
4
2
-1 -2 0 0 0 0
-1 -2 0 0 0 0
3
1 5 1 1 1 1
2 5 0 1 0 1
3 5 1 0 0 0
2
1337 1337 0 1 1 1
1336 1337 1 1 0 1
1
3 5 1 1 1 1

Sample Outputs:
1 -1 -2
1 2 5
0
1 -100000 -100000

Solution (Python):
import sys
INF = 10 ** 5

def solve():
    n = int(sys.stdin.readline())
    xl = yl = -INF
    xu = yu = INF
    for _ in range(n):
        (x, y, a, b, c, d) = map(int, sys.stdin.readline().split())
        if a == 0:
            xl = max(x, xl)
        if b == 0:
            yu = min(y, yu)
        if c == 0:
            xu = min(x, xu)
        if d == 0:
            yl = max(y, yl)
    if xl > xu or yl > yu:
        sys.stdout.write('0\n')
    else:
        sys.stdout.write(f'1 {xl} {yl}\n')

def main():
    q = int(input())
    for _ in range(q):
        solve()
main()

Solution (Python):
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
    sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
MAXC = int(100000.0)
t = int(input())
for i in range(t):
    (maxx, maxy, minx, miny) = (MAXC, MAXC, -MAXC, -MAXC)
    for q in range(int(input())):
        (x, y, f1, f2, f3, f4) = map(int, input().split())
        if not f1:
            minx = max(minx, x)
        if not f2:
            maxy = min(maxy, y)
        if not f3:
            maxx = min(maxx, x)
        if not f4:
            miny = max(miny, y)
    if minx <= maxx and miny <= maxy:
        print(1, minx, miny)
    else:
        print('0')

Solution (Python):
class CodeforcesTask1196CSolution:

    def __init__(self):
        self.result = ''
        self.q = 0
        self.queries = []

    def read_input(self):
        self.q = int(input())
        for x in range(self.q):
            n = int(input())
            query = []
            for y in range(n):
                query.append([int(z) for z in input().split(' ')])
            self.queries.append(query)

    def process_task(self):
        results = []
        for query in self.queries:
            (top, bottom, left, right) = (None, None, None, None)
            for robot in query:
                if not robot[2]:
                    if left is None:
                        left = robot[0]
                    else:
                        left = max(left, robot[0])
                if not robot[3]:
                    if top is None:
                        top = robot[1]
                    else:
                        top = min(top, robot[1])
                if not robot[4]:
                    if right is None:
                        right = robot[0]
                    else:
                        right = min(right, robot[0])
                if not robot[5]:
                    if bottom is None:
                        bottom = robot[1]
                    else:
                        bottom = max(bottom, robot[1])
            if top is None:
                if bottom is None:
                    top = 0
                    bottom = 0
                else:
                    top = bottom
            elif bottom is None:
                bottom = top
            if left is None:
                if right is None:
                    left = 0
                    right = 0
                else:
                    left = right
            elif right is None:
                right = left
            if top >= bottom and right >= left:
                results.append('1 {1} {0}'.format(top, left))
            else:
                results.append('0')
        self.result = '\n'.join(results)

    def get_result(self):
        return self.result
if __name__ == '__main__':
    Solution = CodeforcesTask1196CSolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())