Ela and Crickets (Codeforces 1737C)
Tags: Math, games

Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game.The problem, which involves a non-standard chess pieces type that is described below, reads: given $$$3$$$ white crickets on a $$$n \cdot n$$$ board, arranged in an "L" shape next to each other, there are no other pieces on the board. Ela wants to know with a finite number of moves, can she put any white cricket on the square on row $$$x$$$, column $$$y$$$?An "L"-shape piece arrangement can only be one of the below:    For simplicity, we describe the rules for crickets on the board where only three white crickets are. It can move horizontally, vertically, or diagonally, but only to a square in some direction that is immediately after another cricket piece (so that it must jump over it). If the square immediately behind the piece is unoccupied, the cricket will occupy the square. Otherwise (when the square is occupied by another cricket, or does not exist), the cricket isn't allowed to make such a move.See an example of valid crickets' moves on the pictures in the Note section.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.The first line of each test case contains $$$n$$$ ($$$4 \le n \le 10^5$$$) — denotes the size of the chessboard.The second line of each test case contains 6 numbers: $$$r_1$$$, $$$c_1$$$, $$$r_2$$$, $$$c_2$$$, $$$r_3$$$, $$$c_3$$$ ($$$1 \le r_1, c_1, r_2, c_2, r_3, c_3 \le n$$$) — coordinates of the crickets. The input ensures that the three crickets are arranged in an "L" shape that the legend stated.The third line of each test case contains 2 numbers: $$$x$$$, $$$y$$$ ($$$1 \le x, y \le n$$$) — coordinates of the target square.

Output: For each test case, print "YES" or "NO" to denotes whether Ela can put a cricket on the target square.

Note: Here's the solution for the first test case. The red square denotes where the crickets need to reach. Note that in chess horizontals are counted from bottom to top, as well as on this picture.  

Sample Inputs:
6
8
7 2 8 2 7 1
5 1
8
2 2 1 2 2 1
5 5
8
2 2 1 2 2 1
6 6
8
1 1 1 2 2 1
5 5
8
2 2 1 2 2 1
8 8
8
8 8 8 7 7 8
4 8

Sample Outputs:
YES
NO
YES
NO
YES
YES

Solution (Python):
import sys

def solve():
    inp = sys.stdin.readline
    n = int(inp())
    (r1, c1, r2, c2, r3, c3) = map(int, inp().split())
    if r1 == r2:
        r = r1
    elif r1 == r3:
        r = r1
    elif r2 == r3:
        r = r2
    if c1 == c2:
        c = c1
    elif c1 == c3:
        c = c1
    elif c2 == c3:
        c = c2
    (x, y) = map(int, inp().split())
    if (r == 1 or r == n) and (c == 1 or c == n):
        if x == r or y == c:
            print('YES')
        else:
            print('NO')
    elif (x - r) % 2 == 0 or (y - c) % 2 == 0:
        print('YES')
    else:
        print('NO')
''

def main():
    for i in range(int(sys.stdin.readline())):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
import sys, math

def get_arr():
    return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
    return map(int, sys.stdin.readline().strip().split())
t = int(sys.stdin.readline())
for t2 in range(t):
    n = int(sys.stdin.readline())
    (r1, c1, r2, c2, r3, c3) = get_ints()
    (x, y) = get_ints()
    if r1 == r2:
        row = r1
    elif r1 == r3:
        row = r1
    else:
        row = r2
    if c1 == c2:
        col = c1
    elif c1 == c3:
        col = c1
    else:
        col = c2
    if row == x or col == y:
        print('YES')
        continue
    if row == 1 and col == 1:
        print('NO')
        continue
    elif row == 1 and col == n:
        print('NO')
        continue
    elif row == n and col == n:
        print('NO')
        continue
    elif row == n and col == 1:
        print('NO')
        continue
    if abs(x - row) % 2 != 0 and abs(y - col) % 2 != 0:
        print('NO')
    else:
        print('YES')

Solution (Python):
import sys
input = sys.stdin.readline
from math import sqrt
for _ in range(int(input())):
    n = int(input())
    (r1, c1, r2, c2, r3, c3) = [int(i) for i in input().split()]
    (x, y) = [int(i) for i in input().split()]
    if r1 == r2 or r1 == r3:
        r = r1
    else:
        r = r2
    if c1 == c2 or c1 == c3:
        c = c1
    else:
        c = c3
    if r == 1 and c == 1:
        if x == 1 or y == 1:
            print('YES')
        else:
            print('NO')
        continue
    if r == n and c == n:
        if x == n or y == n:
            print('YES')
        else:
            print('NO')
        continue
    if r == 1 and c == n:
        if x == 1 or y == n:
            print('YES')
        else:
            print('NO')
        continue
    if r == n and c == 1:
        if x == n or y == 1:
            print('YES')
        else:
            print('NO')
        continue
    if x % 2 == r % 2 or y % 2 == c % 2:
        print('YES')
    else:
        print('NO')