#C11570. Snake Game Simulation

    ID: 40901 Type: Default 1000ms 256MiB

Snake Game Simulation

Snake Game Simulation

This problem requires you to implement a simulation of the classic Snake Game. The game board has dimensions \(width \times height\), and a list of food positions is provided. Initially, the snake occupies the cell (0, 0). At each move, the snake will move in one of the four directions: U (up), L (left), R (right), or D (down). When the snake moves:

  • If the new head position is out of bounds or the snake bites itself (collides with its body, except for the cell that is about to be vacated), the game ends and the move should return -1.
  • If the new head position contains food, the snake grows, the food is consumed, and the score increases by 1.
  • If the move is valid but no food is consumed, the snake moves forward normally (its tail moves forward to follow the head).

Your task is to implement the SnakeGame class with an appropriate constructor and a method move(direction) that returns the score after the move or -1 if the game is over.

Note: All formulas are given in LaTeX: the board has dimensions \(width \times height\).

inputFormat

The input is read from stdin with the following format:

  1. The first line contains two integers: width and height.
  2. The second line contains an integer n, the number of food items.
  3. The next n lines each contain two space-separated integers representing the food coordinates.
  4. The next line contains an integer m, the number of moves.
  5. The following m lines each contain a single character representing a move direction (one of U, L, R, D).

outputFormat

For each move, output the score after the move on a new line to stdout. If a move causes the game to end, output -1 for that move.

## sample
3 2
2
1 2
0 1
3
R
D
R
0

0 1

</p>