★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10741729.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Design a Snake game that is played on a device with screen size = width x height. 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 length = 1 unit.
You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
Example:
Given width = 3, height = 2, and food = [[1,2],[0,1]]. Snake snake = new Snake(width, height, food); Initially the snake appears at position (0,0) and the food at (1,2). |S| | | | | |F| snake.move("R"); -> Returns 0 | |S| | | | |F| snake.move("D"); -> Returns 0 | | | | | |S|F| snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) ) | |F| | | |S|S| snake.move("U"); -> Returns 1 | |F|S| | | |S| snake.move("L"); -> Returns 2 (Snake eats the second food) | |S|S| | | |S| snake.move("U"); -> Returns -1 (Game over because snake collides with border)
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
设计一个在屏幕大小=宽x高的设备上玩的蛇游戏。如果您不熟悉游戏,请在线玩游戏。
蛇最初位于左上角(0,0),长度=1单位。
你会得到一份食物位置列表,按行、列顺序排列。当蛇吃了食物,它的长度和游戏的得分都增加了1。
每种食物一个一个地出现在屏幕上。例如,第二种食物在第一种食物被蛇吃之前不会出现。
当一种食物出现在屏幕上时,它保证不会出现在被蛇占据的街区上。
例子:
给定width = 3, height = 2, and food = [[1,2],[0,1]].
Snake snake = new Snake(width, height, food);
最初snake出现在位置(0,0),food出现在位置(1,2)。
|S| | | | | |F| snake.move("R"); -> Returns 0 | |S| | | | |F| snake.move("D"); -> Returns 0 | | | | | |S|F| snake.move("R"); -> Returns 1 (snake吃第一种食物,之后第二种食物出现在(0,1))。 | |F| | | |S|S| snake.move("U"); -> Returns 1 | |F|S| | | |S| snake.move("L"); -> Returns 2 (snake吃第二种食物) | |S|S| | | |S| snake.move("U"); -> Returns -1 (游戏结束,因为snake与边界碰撞)
Credits:
特别感谢@elmirap添加此问题并创建所有测试用例。
Solution:
1 class Snake { 2 /** Initialize your data structure here. 3 @param width - screen width 4 @param height - screen height 5 @param food - A list of food positions 6 E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */ 7 8 var Int 9 var height:Int 10 var score:Int 11 var food:[[Int]] 12 var snake:[[Int]] 13 14 init(_ Int,_ height:Int,_ food:[[Int]]) 15 { 16 self.width = width 17 self.height = height 18 self.food = food 19 self.score = 0 20 self.snake = [[0,0]] 21 } 22 23 /** Moves the snake. 24 @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 25 @return The game's score after the move. Return -1 if game over. 26 Game over when snake crosses the screen boundary or bites its body. */ 27 28 func move(_ direction:String) -> Int 29 { 30 var head:[Int] = snake.first! 31 var tail:[Int] = snake.last! 32 snake.removeLast() 33 if direction == "U" { head[0] -= 1} 34 else if direction == "L" { head[1] -= 1} 35 else if direction == "R" { head[1] += 1} 36 else if direction == "D" { head[0] += 1} 37 if snake.contains(head) || head[0] < 0 || head[0] >= height || head[1] < 0 || head[1] >= width 38 { 39 return -1 40 } 41 snake.insert(head,at:0) 42 if !food.isEmpty && head == food.first! 43 { 44 food.removeFirst() 45 snake.append(tail) 46 score += 1 47 } 48 return score 49 } 50 }
点击:Playground测试
1 let Int = 3 2 let height:Int = 2 3 let food:[[Int]] = [[1,2],[0,1]] 4 var snake = Snake(width, height, food) 5 6 //snake.move("R"); -> Returns 0 7 print(snake.move("R")) 8 //Print 0 9 10 //snake.move("D"); -> Returns 0 11 print(snake.move("D")) 12 //Print 0 13 14 //snake.move("R"); -> Returns 1 15 print(snake.move("R")) 16 //Print 1 17 18 //snake.move("U"); -> Returns 1 19 print(snake.move("U")) 20 //Print 1 21 22 //snake.move("L"); -> Returns 2 23 print(snake.move("L")) 24 //Print 2 25 26 //snake.move("U"); -> Returns -1 27 print(snake.move("U")) 28 //Print -1