A snake game runs on an R by C board (rows 0..R-1, columns 0..C-1). The snake starts as a single cell occupying (0, 0); that cell is both its head and tail. A queue of food cells must be eaten in the given order - only the food at the front of the queue is active. A move string is processed one move at a time (U/D/L/R change the head by one cell: U row-1, D row+1, L col-1, R col+1).
For each move, compute the new head cell, then:
Once the snake dies, no further moves are processed.
Line 1: two integers R and C.
Line 2: an integer F, the number of food cells.
Next F lines: two integers r c each, the food cells in eating order.
Next line: a non-empty move string over U, D, L, R.
A single line: ALIVE k if the snake survives every move, or DEAD k if it dies, where k is the snake's length at the end (the length reached at the moment of death if it dies).
Example 1
Input
3 3 1 0 1 RD
Expected
ALIVE 2
Explanation
Moving right eats the food at (0,1) and the snake grows to length 2; moving down advances the head with the tail following, so it survives at length 2.
Example 2
Input
2 2 0 RDLU
Expected
ALIVE 1
Explanation
With no food the snake stays length 1, tracing the 2x2 board without ever hitting a wall or itself; it survives at length 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →