This page is still under construction.

Parts of this page are still being built. What you see may change.

Stealth Ninja

Time limit1sMemory limit128 MB

Summary
Given guards patrolling a grid with periodic vision, decide whether a ninja can walk from the front wall to the back wall unseen.
Level

Hard8 of 10

Topics
Graph, BFS, Simulation
Solved
No attempts yet

Problem

A large palace contains a hall whose floor is a (2n−1)×(2n−1)(2n-1) \times (2n-1) grid of squares. The rows are numbered from 11 to 2n−12n-1 from the front of the hall to the back, and the columns are numbered from 11 to 2n−12n-1 from the left to the right.

Every square that lies on both an even-numbered row and an even-numbered column is occupied by a square wooden support column; all other squares are empty. As a result the empty squares form nn corridors running left-to-right (along the odd-numbered rows) and nn corridors running front-to-back (along the odd-numbered columns). Each front-to-back corridor ends, at the front wall and at the back wall, in a door opening covered by a curtain. Every other part of the walls is closed.

The hall is guarded by kk guards. Each guard stands at a crossing of a front-to-back corridor and a left-to-right corridor, that is, on a square whose row and column are both odd. A guard spends 44 seconds looking toward the left of the hall, then 44 seconds toward the back, then 44 seconds toward the right, then 44 seconds toward the front, and repeats this 1616-second cycle forever. The guards are not necessarily synchronized: at time 00 each guard may already be looking in any of the four directions. While a guard looks in a given direction it sees every square along its corridor in that direction, all the way to the wall. Guards can see past one another, but they cannot see through a curtain.

A ninja wants to cross the hall from the front to the back without meeting a guard and without ever being seen. Before entering, the ninja waits in row 00, that is, behind one of the curtains in the front wall; it chooses which curtain and how long to wait. It may leave through any curtain in the back wall. Walking from one square to an orthogonally adjacent square takes the ninja 22 seconds, and while walking it would be seen by any guard that, at any moment during that step, is looking at the square it is leaving or the square it is entering. The ninja may never stand on a square that a guard is currently watching, and may never enter a square occupied by a guard or a wooden column.

As an example, take n=4n = 4 with the five guards of the sample case. The ninja can cross as follows. It first waits. After 88 seconds the guard in row 55, column 11 turns to look left, and the ninja steps through the curtain in column 11. After 1010 seconds it reaches row 11, column 11; after 1212 seconds it reaches row 22, column 11. It waits there until the guard in row 33, column 55 turns to look toward the back, then walks on and disappears behind the curtain in column 33 just before the guard in row 11 turns to look in that direction. In this way the ninja succeeds.

For each hall, determine whether the ninja can cross successfully.

Input

The first line contains a single integer: the number of test cases. Each test case has the following format:

  • One line with two integers nn and kk: the number of corridors in each direction (1≤n≤2501 \le n \le 250) and the number of guards (0≤k≤5000 \le k \le 500).
  • kk lines, one per guard. Each line contains an odd integer rr (1≤r≤2n−11 \le r \le 2n-1), an odd integer cc (1≤c≤2n−11 \le c \le 2n-1), and one of the letters L, B, R, or F. Here rr and cc are the row and column of the guard, and the letter is the direction it is looking at time 00 (left, back, right, or front).

No two guards share the same position.

Output

For each test case, print a single line containing either succeeds or fails.

Hint

In the sample case (n=4n = 4 with five guards) the ninja can cross the hall from front to back without being seen, so the answer is succeeds. This is the situation described in the worked example above.

Examples3

  1. Example 1

    Input
    1
    4 5
    1 3 B
    3 5 B
    5 1 R
    5 5 F
    7 7 F
    
    Expected output
    succeeds
    
  2. Example 2

    Input
    1
    1 0
    
    Expected output
    succeeds
    
  3. Example 3

    Input
    1
    1 1
    1 1 F
    
    Expected output
    fails