Design Snake Game

Design a snake game that moves on a grid, eats food, grows, and detects collisions. Pattern focus: Queue for Task Simulation. Keep the snake body in queue order so the head and tail can be updated efficiently.

Input Format

width = board width, height = board height, food = food positions, moves = direction commands

Output Format

score after each move

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • width, height, food, moves must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

width = 3
height = 2
food = [[1,2],[0,1]]
moves = ["R","D","R","U","L","U"]

Output:

[0,0,1,1,2,-1]

Explanation:

The snake grows after eating food and later collides.

Example 2:

Input:

width = 2
height = 2
food = []
moves = ["R","D","L"]

Output:

[0,0,0]

Explanation:

Without food, the snake only moves and survives the given sequence.

Loading...
Design Snake Game - Queue Deque DSA Problem