Phillip and Trains (Codeforces 585B)
Tags: Others, shortest paths

The mobile application store has a new game called "Subway Roller".The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column.  

Input: Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≤ t ≤ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) — the number of sets.Then follows the description of t sets of the input data. The first line of the description of each set contains two integers n, k (2 ≤ n ≤ 100, 1 ≤ k ≤ 26) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains.

Output: For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.

Note: In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.Note that in this problem the challenges are restricted to tests that contain only one testset.

Sample Inputs:
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....

2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...

Sample Outputs:
YES
NO

YES
NO

Solution (Python):
global ff
(R, I) = (range, input)
u = [[0] * 101 for j in R(5)]
v = [[0] * 101 for j in R(5)]

def dfs(x, y):
    global ff
    if y >= m or ff == 1:
        ff = 1
        return
    v[x][y] = 1
    for i in range(-1, 2):
        nx = x + i
        if v[nx][y + 3] == 0 and u[x][y + 1] == 0 and (u[nx][y + 1] == 0) and (u[nx][y + 2] == 0) and (u[nx][y + 3] == 0) and (nx >= 1) and (nx <= 3):
            dfs(nx, y + 3)
for i in R(int(I())):
    (m, k) = map(int, I().split())
    a = [[x for x in I()] for y in R(3)]
    ff = 0
    sh = -1
    u = [[0] * 101 for _ in R(5)]
    v = [[0] * 101 for _ in R(5)]
    for xx in R(3):
        for yy in R(m):
            if a[xx][yy] == 's':
                sh = xx + 1
                v[xx + 1][1] = 1
            elif a[xx][yy] >= 'A' and a[xx][yy] <= 'Z':
                u[xx + 1][yy + 1] = 1
    dfs(sh, 1)
    print('YES' if ff else 'NO')

Solution (Python):
global ff
mp = [[0 for i in range(101)] for j in range(5)]
vis = [[0 for i in range(101)] for j in range(5)]

def dfs(x, y):
    global ff
    if y >= m or ff == 1:
        ff = 1
        return
    vis[x][y] = 1
    for i in range(-1, 2):
        nx = x + i
        if vis[nx][y + 3] == 0 and mp[x][y + 1] == 0 and (mp[nx][y + 1] == 0) and (mp[nx][y + 2] == 0) and (mp[nx][y + 3] == 0) and (nx >= 1) and (nx <= 3):
            dfs(nx, y + 3)
for i in range(int(input())):
    (m, k) = map(int, input().split())
    mat = [[x for x in input()] for y in range(3)]
    ff = 0
    sh = -1
    mp = [[0] * 101 for _ in range(5)]
    vis = [[0] * 101 for _ in range(5)]
    for xx in range(3):
        for yy in range(m):
            if mat[xx][yy] == 's':
                sh = xx + 1
                vis[xx + 1][1] = 1
            elif mat[xx][yy] >= 'A' and mat[xx][yy] <= 'Z':
                mp[xx + 1][yy + 1] = 1
    dfs(sh, 1)
    print('YES' if ff else 'NO')

Solution (Python):
def checkPoint(graph, x, y):
    n = len(graph)
    m = len(graph[0])
    if y >= m:
        return True
    if x >= 0 and x < n and (y >= 0) and (graph[x][y] == '.'):
        return True
    return False

def BFS(graph):
    queue = []
    vis = set()
    x = 0
    y = 0
    ans = False
    m = len(graph[0])
    for i in range(3):
        if graph[i][y] == 's':
            x = i
            break
    queue.append((x, y))
    vis.add((x, y))
    while len(queue) > 0:
        currentPosition = queue.pop(0)
        currentx = currentPosition[0]
        currenty = currentPosition[1]
        if checkPoint(graph, currentx, currenty + 1) and checkPoint(graph, currentx, currenty + 2) and checkPoint(graph, currentx, currenty + 3):
            if currenty + 3 >= m - 1:
                ans = True
                break
            if (currentx, currenty + 3) not in vis:
                queue.append((currentx, currenty + 3))
                vis.add((currentx, currenty + 3))
        if checkPoint(graph, currentx, currenty + 1) and checkPoint(graph, currentx - 1, currenty + 1) and checkPoint(graph, currentx - 1, currenty + 2) and checkPoint(graph, currentx - 1, currenty + 3):
            if currenty + 3 >= m - 1:
                ans = True
                break
            if (currentx - 1, currenty + 3) not in vis:
                queue.append((currentx - 1, currenty + 3))
                vis.add((currentx - 1, currenty + 3))
        if checkPoint(graph, currentx, currenty + 1) and checkPoint(graph, currentx + 1, currenty + 1) and checkPoint(graph, currentx + 1, currenty + 2) and checkPoint(graph, currentx + 1, currenty + 3):
            currentx += 1
            currenty += 3
            if currenty >= m - 1:
                ans = True
                break
            if (currentx, currenty) not in vis:
                queue.append((currentx, currenty))
                vis.add((currentx, currenty))
    return ans
t = int(input())
while t > 0:
    t -= 1
    s = input().split()
    n = int(s[0])
    k = int(s[1])
    graph = []
    k = 3
    while k > 0:
        k -= 1
        s = input()
        graph.append(s)
    ans = BFS(graph)
    if ans == True:
        print('YES')
    else:
        print('NO')