All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There is a rectangular grid of cells consisting of n rows and m columns. You will place a robot on one of the grid cells and provide it with a command string s, consisting of characters ‘L’, ‘R’, ‘U’, ‘D’. After being placed, the robot 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 robot in one of the grid cells initially and have it always stay entirely within the grid upon execution of the command string s. Output “safe” if there is a starting cell for which the robot doesn’t fall off the grid on following command s, otherwise, output "unsafe".
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.
For each test case, output "safe" (without quotes) or "unsafe" (without quotes) in a new line.
- 1 ≤ T ≤ 1,000
- 1 ≤ n,m ≤ 10
- 1 ≤ |s| ≤ 10
Input: 5 1 1 R 2 3 LLRU 3 2 LLRU 4 3 ULURUDRDLD 3 6 RURUR Output: unsafe safe unsafe safe safe
For the first case, there is only one grid square, so we must place our robot there. When the robot follows the command, it'll fall off, so it is unsafe.
For the second case, we can place the robot on the bottom right grid square. Here is an image denoting the moves that the robot will make.
|Tags||bruteforce cook80 easy lg5293 string|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.9.2, CPP14, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYPY, PYTH, PYTH 3.4, RUBY, SCALA, SCM chicken, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC|
Fetching successful submissions