LC: 353. Design Snake Game
https://leetcode.com/problems/design-snake-game/
353. Design Snake Game
Design a Snake game that is played on a device with screen size height x width. Play the game online if you are not familiar with the game.
The snake is initially positioned at the top left corner (0, 0) with a length of 1 unit.
You are given an array food where food[i] = (ri, ci) is the row and column position of a piece of food that the snake can eat. When a snake eats a piece of food, its length and the game's score both increase by 1.
Each piece of food appears one by one on the screen, meaning the second piece of food will not appear until the snake eats the first piece of food.
When a piece of food appears on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
The game is over if the snake goes out of bounds (hits a wall) or if its head occupies a space that its body occupies after moving (i.e. a snake of length 4 cannot run into itself).
Implement the SnakeGame class:
SnakeGame(int width, int height, int[][] food)Initializes the object with a screen of sizeheight x widthand the positions of thefood.int move(String direction)Returns the score of the game after applying onedirectionmove by the snake. If the game is over, return-1.
Example 1:
Input
["SnakeGame", "move", "move", "move", "move", "move", "move"]
[[3, 2, [[1, 2], [0, 1]]], ["R"], ["D"], ["R"], ["U"], ["L"], ["U"]]
Output
[null, 0, 0, 1, 1, 2, -1]
Explanation
SnakeGame snakeGame = new SnakeGame(3, 2, [[1, 2], [0, 1]]);
snakeGame.move("R"); // return 0
snakeGame.move("D"); // return 0
snakeGame.move("R"); // return 1, snake eats the first piece of food. The second piece of food appears
// at (0, 1).
snakeGame.move("U"); // return 1
snakeGame.move("L"); // return 2, snake eats the second food. No more food appears.
snakeGame.move("U"); // return -1, game over because snake collides with borderConstraints:
1 <= width, height <= 1041 <= food.length <= 50food[i].length == 20 <= ri < height0 <= ci < widthdirection.length == 1directionis'U','D','L', or'R'.At most
104calls will be made tomove.
The Essence:
Zu beachten ist, dass die Schlange nur eine Menge von Punkten auf dem Spielgitter ist. Wenn sich die Schlange in eine Richtung bewegt, endet das Spiel, wenn die Schlange die Grenzen des Gitters berührt oder wenn zwei Punkte in der Schlange gleich sind. Die Schlange kann kein anderes Futterhappen fressen, bevor sie das mit einem vorherigen Index frisst. Dieses Problem konzentriert sich insbesondere auf den Entwurf von Klassen für interagierende/kommunizierende Systeme. Bei diesem Problem kann der Problemlöser auch sehen, wie Komponenten eines dynamischen Systems beim Eingaben des Benutzers miteinander interagieren. Das Umgehen mit solchen Problemen ist eine nützliche Fähigkeit, insbesondere beim Entwerfen dynamischer Websites und Videospiele.
Details:
Die Schlange kann als verkettete Liste von Punkten oder als Boolean-Matrix dargestellt werden, wobei letzteres schwieriger zu implementieren ist. Das Futterhappen kann mit dem Index in Bezug auf die Anzahl der gefressenen Happen aktualisiert werden
Solution(s) and Further Explanation:
Default Code:
Last updated