Trace Path
Submissions: 364   Accuracy:

21.48%

  Difficulty: Easy   Marks: 2

There is the Rectangular path for a Train to travel consisting of n and m rows and columns respectively. The train will start from one of grid cells and it will be given a command in the form of String s. consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.The train will follow the instructions of the command string, where 'L' corresponds moving to the left, 'R' towards the right, 'U' for moving up, and 'D' means down.
You have already selected the command string s, and are wondering if it is possible to place the train in one of the grid cells initially and have it always stay entirely within the grid upon execution of the command string s. Output “1” if there is a starting cell for which the train doesn’t fall off the grid(track) on following command s, otherwise, output "0".

Input:
The first line of input will contain an integer T, the number of test cases.
Each test case will be on two lines.
The first line will have two space separated integers n,m.
The second line will have the command string s.

Output:
For each test case, output "1" (without quotes) or "0" (without quotes) in a new line.

Constraints:
1 <= T <= 1,00
1 <= n,m <= 10
1 <= |s| <= 10

Example:
Input:

2
1 1
R
2 3
LLRU
Output:
0
1

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth
Author: saksham seth


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.