Mandarin chinese
, Russian and Vietnamese as well.
You are given a grid with $n$ rows and $m$ columns. Each cell of this grid can either be empty or it contains one particle. It can never contain more than one particle. Let's denote the cell in the $i$-th row and $j$-th column by $(i, j)$, with the top left corner being $(0, 0)$. From a cell $(i, j)$, a particle could move in one of the following four directions:
- to the left, i.e. to the cell $(i, j - 1)$
- to the right, i.e. to the cell $(i, j + 1)$
- up, i.e. to the cell $(i - 1, j)$
- down, i.e. to the cell $(i + 1, j)$
It is not possible for a particle to move to a cell that already contains a particle or to a cell that does not exist (leave the grid).
It is possible to apply a force in each of these directions. When a force is applied in a given direction, all particles will simultaneously start moving in this direction as long as it is still possible for them to move.
You are given a sequence of forces. Each subsequent force is applied only after all particles have stopped moving. Determine which cells of the grid contain particles after all forces from this sequence are applied in the given order.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $n$ and $m$.
- $n$ lines describing the initial grid follow. For each $i$ ($1 \le i \le n$), the $i$-th of these lines contains a binary string with length $m$ describing the $i$-th row of the grid. For each $j$ ($1 \le j \le m$), if the $j$-th character of this string is '1', then the cell $(i, j)$ contains a particle, and if it is '0', then the cell $(i, j)$ is empty.
- The last line contains a single string $S$ describing the sequence of applied forces. Each character of this string corresponds to applying a force in some direction; forces applied in the directions left, right, up, down correspond to characters 'L', 'R', 'U', 'D' respectively.

-----Output-----
For each test case, print $n$ lines each containing a binary string of length $m$, describing the resulting grid (after all the forces are applied) in the same format as the input grid.

-----Constraints-----
- $1 \le T \le 200$
- $1 \le n, m \le 100$
- $1 \le |S| \le 2 \cdot 10^4$

-----Subtasks-----
Subtaks #1 (30 points):
- $1 \le T \le 10$
- $1 \le n, m \le 10$
- $1 \le |S| \le 100$
Subtask #2 (70 points): Original constraints

-----Example Input-----
3
4 4
1010
0010
1001
0100
LRDU
4 3
000
010
001
101
LRL
3 2
01
10
00
D

-----Example Output-----
0011
0011
0001
0001
000
100
100
110
00
00
11

-----Explanation-----
Example case 1: The initial grid is:
1010
0010
1001
0100

After applying the first force (in the direction "L", i.e. to the left), the grid is:
1100
1000
1100
1000

After applying the second force (in the direction "R"), the grid is:
0011
0001
0011
0001

After applying the third force (in the direction "D"), the grid is:
0001
0001
0011
0011

After applying the fourth force (in the direction "U"), the final grid is:
0011
0011
0001
0001

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
def main():
	for _ in range(int(input())):
		(rows, column) = map(int, input().split())
		arr = []
		for i in range(rows):
			arr.append(list(input()))
		string = input()
		last = string[-1]
		operation = Find(string, last)
		for i in string[0] + operation:
			if i == 'L':
				arr = Left(arr)
			if i == 'R':
				arr = Right(arr)
			if i == 'U':
				arr = Transpose(arr)
				arr = Left(arr)
				arr = Transpose(arr)
			if i == 'D':
				arr = Transpose(arr)
				arr = Right(arr)
				arr = Transpose(arr)
		for i in arr:
			print(i)

def Left(arr):
	for i in range(len(arr)):
		ans = arr[i].count('1')
		arr[i] = '1' * ans + (len(arr[i]) - ans) * '0'
	return arr

def Right(arr):
	for i in range(len(arr)):
		ans = arr[i].count('1')
		arr[i] = (len(arr[i]) - ans) * '0' + '1' * ans
	return arr

def Transpose(arr):
	ansss = []
	ans = list(map(list, zip(*arr)))
	for i in ans:
		ass = i
		hello = ''
		for j in ass:
			hello += j
		ansss.append(hello)
	return ansss

def Find(string, last):
	for i in string[-2::-1]:
		if last == 'L':
			if i in ['D', 'U']:
				last = i + last
				break
		if last == 'R':
			if i in ['D', 'U']:
				last = i + last
				break
		if last == 'D':
			if i in ['L', 'R']:
				last = i + last
				break
		if last == 'U':
			if i in ['L', 'R']:
				last = i + last
				break
	return last

def __starting_point():
	main()
__starting_point()


def left(list1):
	for i in range(len(list1)):
		list2 = sorted(list1[i])
		list2.reverse()
		list1[i] = list2

def right(list1):
	for i in range(len(list1)):
		list2 = sorted(list1[i])
		list1[i] = list2

def up(list1):
	for i in range(len(list1[0])):
		list2 = []
		for j in range(len(list1)):
			list2.append(list1[j][i])
		list2.sort()
		list2.reverse()
		for j in range(len(list1)):
			list1[j][i] = list2[j]

def down(list1):
	for i in range(len(list1[0])):
		list2 = []
		for j in range(len(list1)):
			list2.append(list1[j][i])
		list2.sort()
		for j in range(len(list1)):
			list1[j][i] = list2[j]
test_case = int(input())
result = []
for i in range(test_case):
	[n, m] = list(map(int, input().split()))
	list1 = []
	for i in range(n):
		y = list(input())
		str2 = ' '.join(y)
		list1.append(list(map(int, str2.split())))
	op = list(input())
	for i in range(len(op)):
		if op[i] == 'L':
			left(list1)
		if op[i] == 'R':
			right(list1)
		if op[i] == 'D':
			down(list1)
		if op[i] == 'U':
			up(list1)
	result.append(list1)
for char in result:
	for el in char:
		print(''.join(list(map(str, el))))