Two Semiknights Meet (Codeforces 362A)
Tags: Greedy, math

A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.Please see the test case analysis.

Input: The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.

Output: For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.

Note: Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).On the second board the semiknights will never meet. 

Sample Inputs:
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......
........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#

Sample Outputs:
YES
NO

Solution (Python):
n = int(input())
testcases = []
for i in range(n):
    chess = []
    for _ in range(8):
        points = input()
        chess.append(points)
    if i != n - 1:
        new_line = input()
    testcases.append(chess)
for chess in testcases:

    def main():
        poss_k = []
        for i in range(8):
            for j in range(8):
                if chess[i][j] == 'K':
                    poss_k.append((i, j))
        (k1, k2) = sorted(poss_k)
        i = 0
        if k1[i] % 4 != k2[i] % 4:
            return False
        i = 1
        if k1[i] % 4 != k2[i] % 4:
            return False
        if k2[0] - k1[0] == 8 and k2[1] - k1[1] == 8:
            return True
        movs_u = (k2[1] - k1[1]) // 2
        movs_r = (k2[0] - k1[0]) // 2
        if movs_u % 2 == 1 and movs_r % 2 == 1:
            return False
        return True
    if main():
        print('YES')
    else:
        print('NO')

Solution (Python):
def dfs(position, end, mark, parity):
    mark[position[0]][position[1]] = parity
    row = position[0] + 2
    col = position[1] + 2
    if row >= 0 and row < 8 and (col >= 0) and (col < 8) and (mark[row][col] == -1):
        dfs((row, col), end, mark, (parity + 1) % 2)
    row = position[0] + 2
    col = position[1] - 2
    if row >= 0 and row < 8 and (col >= 0) and (col < 8) and (mark[row][col] == -1):
        dfs((row, col), end, mark, (parity + 1) % 2)
    row = position[0] - 2
    col = position[1] + 2
    if row >= 0 and row < 8 and (col >= 0) and (col < 8) and (mark[row][col] == -1):
        dfs((row, col), end, mark, (parity + 1) % 2)
    row = position[0] - 2
    col = position[1] - 2
    if row >= 0 and row < 8 and (col >= 0) and (col < 8) and (mark[row][col] == -1):
        dfs((row, col), end, mark, (parity + 1) % 2)

def solve():
    t = int(input().strip())
    for ti in range(t):
        if ti > 0:
            _ = input()
        knights = []
        board = []
        mark = []
        for i in range(0, 8):
            line = input().strip()
            board.append([c for c in line])
            mark.append([-1 for c in line])
            for j in range(len(line)):
                if line[j] == 'K':
                    knights.append((i, j))
        dfs(knights[0], knights[1], mark, 0)
        print('YES' if mark[knights[1][0]][knights[1][1]] == 0 else 'NO')
solve()

Solution (Python):
n = int(input())
for l in range(n):
    pos = []
    for i in range(8):
        s = input()
        for j in range(8):
            if s[j] == 'K':
                pos.append((i, j))
    print('YES' if abs(pos[0][0] - pos[1][0]) % 4 == abs(pos[0][1] - pos[1][1]) % 4 == 0 else 'NO')
    if l != n - 1:
        input()