|
| 1 | +<p>Design a <a href="https://en.wikipedia.org/wiki/Snake_(video_game)" target="_blank">Snake game</a> that is played on a device with screen size <code>height x width</code>. <a href="http://patorjk.com/games/snake/" target="_blank">Play the game online</a> if you are not familiar with the game.</p> |
| 2 | + |
| 3 | +<p>The snake is initially positioned at the top left corner <code>(0, 0)</code> with a length of <code>1</code> unit.</p> |
| 4 | + |
| 5 | +<p>You are given an array <code>food</code> where <code>food[i] = (r<sub>i</sub>, c<sub>i</sub>)</code> 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 <code>1</code>.</p> |
| 6 | + |
| 7 | +<p>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.</p> |
| 8 | + |
| 9 | +<p>When a piece of food appears on the screen, it is <strong>guaranteed</strong> that it will not appear on a block occupied by the snake.</p> |
| 10 | + |
| 11 | +<p>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 <strong>after</strong> moving (i.e. a snake of length 4 cannot run into itself).</p> |
| 12 | + |
| 13 | +<p>Implement the <code>SnakeGame</code> class:</p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li><code>SnakeGame(int width, int height, int[][] food)</code> Initializes the object with a screen of size <code>height x width</code> and the positions of the <code>food</code>.</li> |
| 17 | + <li><code>int move(String direction)</code> Returns the score of the game after applying one <code>direction</code> move by the snake. If the game is over, return <code>-1</code>.</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong class="example">Example 1:</strong></p> |
| 22 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/13/snake.jpg" style="width: 800px; height: 302px;" /> |
| 23 | +<pre> |
| 24 | +<strong>Input</strong> |
| 25 | +["SnakeGame", "move", "move", "move", "move", "move", "move"] |
| 26 | +[[3, 2, [[1, 2], [0, 1]]], ["R"], ["D"], ["R"], ["U"], ["L"], ["U"]] |
| 27 | +<strong>Output</strong> |
| 28 | +[null, 0, 0, 1, 1, 2, -1] |
| 29 | + |
| 30 | +<strong>Explanation</strong> |
| 31 | +SnakeGame snakeGame = new SnakeGame(3, 2, [[1, 2], [0, 1]]); |
| 32 | +snakeGame.move("R"); // return 0 |
| 33 | +snakeGame.move("D"); // return 0 |
| 34 | +snakeGame.move("R"); // return 1, snake eats the first piece of food. The second piece of food appears at (0, 1). |
| 35 | +snakeGame.move("U"); // return 1 |
| 36 | +snakeGame.move("L"); // return 2, snake eats the second food. No more food appears. |
| 37 | +snakeGame.move("U"); // return -1, game over because snake collides with border |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= width, height <= 10<sup>4</sup></code></li> |
| 45 | + <li><code>1 <= food.length <= 50</code></li> |
| 46 | + <li><code>food[i].length == 2</code></li> |
| 47 | + <li><code>0 <= r<sub>i</sub> < height</code></li> |
| 48 | + <li><code>0 <= c<sub>i</sub> < width</code></li> |
| 49 | + <li><code>direction.length == 1</code></li> |
| 50 | + <li><code>direction</code> is <code>'U'</code>, <code>'D'</code>, <code>'L'</code>, or <code>'R'</code>.</li> |
| 51 | + <li>At most <code>10<sup>4</sup></code> calls will be made to <code>move</code>.</li> |
| 52 | +</ul> |
0 commit comments