Phillip and Trains (Codeforces 586D)
Tags: DP, 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):
def solve(n, k, arr, pos):
    i = 0
    positions = [pos]
    while i < n - 3:
        previousPositions = positions[:]
        positions = []
        i += 1
        for m in range(3):
            if arr[m][i] == '.' and arr[m][i + 2] == '.':
                for q in previousPositions:
                    if abs(m - q) <= 1 and arr[q][i] == '.':
                        if m not in positions:
                            positions.append(m)
        i += 2
    for q in positions:
        for u in range(i, n):
            if arr[q][u] != '.':
                positions.remove(q)
                break
    if positions != []:
        print('YES')
    else:
        print('NO')
t = int(input())
for x in range(t):
    arr = []
    (n, k) = map(int, input().split())
    for i in range(3):
        line = list(input())
        arr.append(line)
        if 's' in line:
            pos = i
    solve(n, k, arr, pos)

Solution (Python):
t = int(input())
cmi = [0, -1, 1]

def dfs(i, j, data, n):
    stacki = list()
    stackj = list()
    stacki.append(i)
    stackj.append(j)
    while len(stacki) != 0:
        ti = stacki.pop()
        tj = stackj.pop()
        for i in range(0, 3):
            nexti = ti + cmi[i]
            nextj = tj + 3
            if not (0 <= nexti and nexti <= 2):
                continue
            isb = True
            for nn in [(ti, tj + 1), (nexti, tj + 1), (nexti, tj + 2), (nexti, tj + 3)]:
                (tti, ttj) = nn
                if ttj < n and data[tti][ttj] == False:
                    isb = False
                    break
            if isb == False:
                continue
            if nextj >= n:
                return True
            else:
                data[nexti][nextj] = False
                stacki.append(nexti)
                stackj.append(nextj)
    return False
for i in range(0, t):
    (n, k) = map(int, input().split())
    si = 0
    sj = 0
    data = list()
    for j in range(0, 3):
        s = input()
        tmp = list()
        for k in range(0, n):
            if s[k] == 's':
                si = j
                sj = k
                tmp.append(True)
            elif s[k] != '.':
                tmp.append(False)
            else:
                tmp.append(True)
        data.append(tmp)
    print(['NO', 'YES'][dfs(si, sj, data, n)])

Solution (Python):
T = int(input())
for t in range(T):
    (n, k) = map(int, input().split(' ')[:2])
    s = ['', '', '']
    for i in range(3):
        s[i] = input()
    s[0] += '.' * (n * 3)
    s[1] += '.' * (n * 3)
    s[2] += '.' * (n * 3)

    def top():
        return [s[0][0] != '.', s[1][0] != '.', s[2][0] != '.']

    def shift():
        s[0] = s[0][1:]
        s[1] = s[1][1:]
        s[2] = s[2][1:]
        return top()
    p = [s[0][0] == 's', s[1][0] == 's', s[2][0] == 's']
    for i in range(1, n):
        np = [False, False, False]
        if p[0] == True and s[0][1] == '.':
            np[0] = True
            np[1] = True
        if p[1] == True and s[1][1] == '.':
            np[0] = True
            np[1] = True
            np[2] = True
        if p[2] == True and s[2][1] == '.':
            np[1] = True
            np[2] = True
        p = np
        (s0, s1, s2) = shift()
        if s0:
            p[0] = False
        if s1:
            p[1] = False
        if s2:
            p[2] = False
        (s0, s1, s2) = shift()
        if s0:
            p[0] = False
        if s1:
            p[1] = False
        if s2:
            p[2] = False
        (s0, s1, s2) = shift()
        if s0:
            p[0] = False
        if s1:
            p[1] = False
        if s2:
            p[2] = False
    if p[0] or p[1] or p[2]:
        print('YES')
    else:
        print('NO')