Dacey the Dice is lost in the twisted forest and needs help
to find a way back home. Dacey the Dice moves by rolling over
from one side to the next along dark and petrifying pathways.
Dacey’s goal is to make a safe return to Dacey’s Home Sweet
Home, which is a safeguarded by a magnetic door.
The twisted forest is laid out as an grid, where each of the
locations
corresponds to an intersection in the forest. Dacey starts his
frightful journey at one of the locations with dot at the top and dots facing right. This implies
there are dots at the
bottom, dots facing
left, dots facing
upwards, and dots
facing downwards. (Note that the two numbers of opposite faces
of a die always add up to . Also, the only side that you
cannot see is the bottom.) Every dot on Dacey the Dice contains
a powerful magnet.
Dacey’s Home Sweet Home is underground and is located at one
of the
intersections. Dacey’s Home Sweet Home is secured by a door
containing four magnets. The magnetic door will only open when
Dacey is standing on top of the door with the side with five
dots facing downwards. If Dacey stands on the door with any
other side facing downwards, the door won’t open, but Dacey can
still move past.
Some of the
locations are occupied by a wicked gigantic magnet which, if
ever stepped on by Dacey, immediately will turn Dacey into a
petrified immovable monument. Dacey’s initial location and
Dacey’s Home Sweet Home are never occupied by a wicked gigantic
magnet.
Dacey the Dice can only move by rolling over, which changes
both Dacey’s position and orientation. For example, if Dacey
has dot at the top and
dots facing left, and
rolls right to the adjacent location, Dacey will afterwards
have dot facing right
and dots at the top.
It takes exactly one roll to move between two adjacent
locations. Dacey cannot roll diagonally and cannot roll outside
the twisted forest.
Your task is to determine whether Dacey can reach the
comfort of the inside of Dacey’s Home Sweet Home or not.
Input Format
The first line contains a single integer giving the number of test
cases. Each case starts with a line containing an integer
(), the length of the
sides of the twisted forest. The next lines containing characters each, describing the
twisted forest. Each location in the forest is described by one
of the following characters:
-
. – Normal location
-
* – Wicked gigantic magnet
-
S – Dacey’s initial
location
-
H – Dacey’s Home Sweet
Home
There will be exactly one cell for Dacey’s initial location,
and exactly one cell for Dacey’s Home Sweet Home.
Output Format
For each test case, output a single line with “Yes” if Dacey can successfully can get inside
Dacey’s Home Sweet Home, or “No”
otherwise.
Sample Input 1 |
Sample Output 1 |
5
2
*.
HS
2
*.
SH
2
S.
.H
3
S..
...
..H
3
S..
***
..H
|
Yes
No
No
Yes
No
|