Robots (Codeforces 1680B)
Tags: Implement, implementation

There is a field divided into $$$n$$$ rows and $$$m$$$ columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R).You can send a command to all robots at the same time. The command can be of one of the four types:  move up;  move right;  move down;  move left. When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction.You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode?

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases.Each test case starts with a line containing two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5$$$) — the number of rows and the number of columns, respectively. Then $$$n$$$ lines follow; each of them contains a string of $$$m$$$ characters. Each character is either E (empty cell} or R (robot).Additional constraint on the input: in each test case, there is at least one robot on the field.

Output: If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.

Note: Explanations for test cases of the example:  in the first test case, it is enough to send a command to move left.  in the second test case, if you try to send any command, at least one robot explodes.  in the third test case, it is enough to send a command to move left.  in the fourth test case, there is already a robot in the upper left corner.  in the fifth test case, the sequence "move up, move left, move up" leads one robot to the upper left corner;  in the sixth test case, if you try to move any robot to the upper left corner, at least one other robot explodes. 

Sample Inputs:
6
1 3
ERR
2 2
ER
RE
2 2
ER
ER
1 1
R
4 3
EEE
EEE
ERR
EER
3 3
EEE
EER
REE

Sample Outputs:
YES
NO
YES
YES
YES
NO

Solution (Python):
from sys import stdin, stdout

def solve(a, n, m):
    (top, left) = ([], [])
    at = list(map(lambda x: ''.join(list(x)), zip(*a)))
    for i in range(n):
        if 'R' in a[i]:
            top = [i, a[i].find('R')]
            break
    for i in range(m):
        if 'R' in at[i]:
            left = [i, at[i].find('R')]
            break
    return 'YES' if top[1] == left[0] and top[0] == left[1] else 'NO'
for i in range(int(stdin.readline().strip())):
    (n, m) = map(int, stdin.readline().strip().split())
    a = [stdin.readline().strip() for _ in range(n)]
    out_ = solve(a, n, m)
    stdout.write(f'{out_}\n')

Solution (Python):
from sys import stdin

def main():
    input = lambda : stdin.readline()[:-1]
    T = int(input())
    for _ in [0] * T:
        (N, M) = map(int, input().split())
        S = [input() for _ in [0] * N]
        n = 0
        for s in S:
            for (i, t) in enumerate(s):
                if t == 'R':
                    n = i
                    break
            else:
                continue
            break
        for s in S:
            for t in s[:n]:
                if t == 'R':
                    print('NO')
                    break
            else:
                continue
            break
        else:
            print('YES')
main()

Solution (Python):
import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(1, T + 1):
    (n, m) = list(map(int, input().split()))
    up = -1
    left = m + 1
    linenumber = 0
    for i in range(n):
        line = input()
        first = line.find('R')
        if first != -1:
            if up == -1:
                up = linenumber
                upcol = first
            if first < left:
                left = first
                leftrow = linenumber
        linenumber += 1
    if up == -1:
        print('NO')
    elif upcol == left or leftrow == up:
        print('YES')
    else:
        print('NO')