Yut Nori Board Check (Small)

Time limit5sMemory limit512 MB

Summary
Decide whether the recorded throw sequence can produce the given board under the stated Yut Nori movement, capture, and shortcut rules.
Level

Medium7 of 10

Topics
Backtracking, Simulation, Brute force
Solved
No attempts yet

Problem

Yut nori is a Korean folk board game. Players throw half moon shaped sticks and move pieces around a board. Two teams throw in turn, and the team that gets all of its pieces past the finish wins. The rules differ from region to region, so assume only the rules written below.

  • Do: move one space forward.
  • Gae: move two spaces forward.
  • Gul: move three spaces forward.
  • Yut: move four spaces forward, then throw again.
  • Mo: move five spaces forward, then throw again.

On each throw the team picks one of its movable pieces and moves it that many spaces. Some piece has to move. Picking a piece that has not started yet puts it on the board and counts from the first space. The throws are applied in the recorded order. If a Mo comes out and then a Gul, moving three spaces first and five spaces second is not possible.

When a piece lands on a space that holds another piece of the same team, the two are stacked and move together from the next move on. When a piece lands on a space that holds a piece of the other team, it captures that piece and the capturing team throws again. A captured piece starts over from the beginning. Capturing with a Yut or a Mo grants one extra throw, not two. A piece that is not on the board yet cannot be captured.

The spaces are numbered 0 to 28. The outer ring runs from 0 to 19, and 0 is both the start and the finish. Spaces 20 to 28 are the shortcuts, and 22 is the center of the board. A piece that stops on 5, 10 or 22 takes the shortcut on its next move. A piece that only passes through those three spaces stays on the lane it is already on. The spaces ahead of each stopping space come in this order.

Stopping spaceSpaces ahead, in order
a piece that has not started1, 2, 3, 4, 5
1 to 4, 6 to 9, 11 to 14, 16 to 19the numbers increasing by 1 up to 19, then 0
520, 21, 22, 23, 24, 15, 16, 17, 18, 19, 0
1025, 26, 22, 27, 28, 0
2021, 22, 23, 24, 15, 16, 17, 18, 19, 0
2122, 23, 24, 15, 16, 17, 18, 19, 0
2227, 28, 0
2324, 15, 16, 17, 18, 19, 0
2415, 16, 17, 18, 19, 0
2526, 22, 27, 28, 0
2622, 27, 28, 0
2728, 0
280

A piece counts as finished only once it goes completely past space 0. From 19 a single space puts the piece on 0, which is not finished yet, so it can still be captured there. Getting a piece home from 19 therefore takes two or more spaces. A piece standing on 0 goes past the finish on its next move, whatever the throw is. A finished piece is not used again. The moment a team gets all of its pieces past the finish, that team wins and the game stops right away. If the winning throw is a Yut or a Mo, no further throw happens after the game stops.

Yong's family split into team A and team B, and team A started. They wrote down every throw on a sheet of paper, in order.

While everyone was away at dinner, the puppy Puppy scattered the pieces. Pieces that had not started and pieces that had already finished sat off the board and stayed as they were, but the positions of the pieces on the board can no longer be trusted. Puppy also chewed away the part of the paper that recorded which team made each throw. What is left is the full list of throws and their order, and all NN of them were really thrown.

Yong rebuilt the board from memory. Decide whether the rebuilt board is consistent with the list of throws on the paper and their order. A game still in progress and a game that has just ended on the last throw both count as consistent. Only the positions of the pieces on the board are given. The number of pieces that have not started and the number that have finished are not given.

Input

The first line has the number of test cases TT.

Each test case takes four lines. The first line has four integers UU, NN, AA, BB separated by spaces. UU is the number of pieces one team uses, NN is the number of throws, AA is the number of team A pieces on the board, and BB is the number of team B pieces on the board.

The second line has the NN throws in the order they came out, separated by spaces. Each throw is one of Do, Gae, Gul, Yut, Mo.

The third line has the AA positions of the team A pieces and the fourth line has the BB positions of the team B pieces, separated by spaces. A line is empty when the count is 0. When pieces are stacked on one space, that space number appears once per piece.

Limits

  • 1≤T≤501 \le T \le 50
  • 1≤U≤21 \le U \le 2
  • 1≤N≤201 \le N \le 20
  • 0≤A≤U0 \le A \le U, 0≤B≤U0 \le B \le U
  • Every position is between 0 and 28.

Output

For each test case print one line in the form Case #x: y. xx is the test case number starting at 1, and yy is the verdict. Print YES when the board can be produced by the given throws in the given order, and NO otherwise.

Examples3

  1. Example 1

    Input
    7
    1 5 1 1
    Do Gae Gul Do Gae
    6
    3
    1 2 1 1
    Gae Gae
    2
    2
    1 2 0 1
    Gae Gae
    
    2
    1 6 1 0
    Do Mo Mo Mo Mo Gae
    1
    
    1 5 1 1
    Mo Gul Gul Do Gul
    27
    6
    2 5 2 1
    Do Gul Do Gae Gae
    1 3
    5
    2 3 2 1
    Do Gae Gul
    1 3
    2
    
    Expected output
    Case #1: YES
    Case #2: NO
    Case #3: YES
    Case #4: NO
    Case #5: YES
    Case #6: NO
    Case #7: YES
    
  2. Example 2

    Input
    5
    1 1 1 0
    Do
    1
    
    1 1 0 0
    Do
    
    
    1 1 1 0
    Mo
    5
    
    1 1 0 1
    Gae
    
    2
    1 2 1 0
    Yut Do
    5
    
    
    Expected output
    Case #1: YES
    Case #2: NO
    Case #3: YES
    Case #4: NO
    Case #5: YES
    
  3. Example 3

    Input
    7
    1 2 1 0
    Mo Gul
    22
    
    1 3 1 0
    Mo Mo Do
    15
    
    1 4 1 0
    Mo Mo Mo Do
    0
    
    1 2 1 0
    Mo Gae
    21
    
    1 4 1 1
    Mo Gul Do Do
    27
    1
    1 3 1 1
    Mo Do Do
    20
    1
    1 3 1 0
    Mo Mo Do
    22
    
    
    Expected output
    Case #1: YES
    Case #2: YES
    Case #3: YES
    Case #4: YES
    Case #5: YES
    Case #6: YES
    Case #7: NO