[LeetCode] Design Snake Game 设计贪吃蛇游戏
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.
感觉最近LeetCode经常出一些design类的题目啊,难道算法类的题目都出完了吗,这道题让我们设计一个贪吃蛇的游戏,这是个简化版的,但是游戏规则还是保持不变,蛇可以往上下左右四个方向走,吃到食物就会变长1个,如果碰到墙壁或者自己的躯体,游戏就会结束。我们需要一个一维数组来保存蛇身的位置,由于蛇移动的过程的蛇头向前走一步,蛇尾也跟着往前,中间的躯体还在原来的位置,所以移动的结果就是,蛇头变到新位置,去掉蛇尾的位置即可。需要注意的是去掉蛇尾的位置是在检测和蛇身的碰撞之前还是之后,如果是之后则无法通过这个test case:[[3,3,[[2,0],[0,0]]],["D"],["D"],["U"]],如果是之前就没有问题了,检测蛇头和蛇身是否碰撞使用的是count(snake.begin(), snake.end(), head),总体来说不算一道难题,参见代码如下:
class SnakeGame {
public:
/** Initialize your data structure here.
@param width - screen width
@param height - screen height
@param food - A list of food positions
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
SnakeGame(int width, int height, vector<pair<int, int>> food) {
this->width = width;
this->height = height;
this->food = food;
score = ;
snake.push_back({, });
} /** Moves the snake.
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
@return The game's score after the move. Return -1 if game over.
Game over when snake crosses the screen boundary or bites its body. */
int move(string direction) {
auto head = snake.front(), tail = snake.back();
snake.pop_back();
if (direction == "U") --head.first;
else if (direction == "L") --head.second;
else if (direction == "R") ++head.second;
else if (direction == "D") ++head.first;
if (count(snake.begin(), snake.end(), head) || head.first < || head.first >= height || head.second < || head.second >= width) {
return -;
}
snake.insert(snake.begin(), head);
if (!food.empty() && head == food.front()) {
food.erase(food.begin());
snake.push_back(tail);
++score;
}
return score;
} private:
int width, height, score;
vector<pair<int, int>> food, snake;
};
参考资料:
https://leetcode.com/problems/design-snake-game/
https://leetcode.com/discuss/106235/c-solution-seems-test-case-didnt-consider-food-on-body
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Design Snake Game 设计贪吃蛇游戏的更多相关文章
- [Swift]LeetCode353. 设计贪吃蛇游戏 $ Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- 基于React的贪吃蛇游戏的设计与实现
代码地址如下:http://www.demodashi.com/demo/11818.html 贪吃蛇小游戏(第二版) 一年半前层用react写过贪吃蛇小游戏https://github.com/ca ...
- WebGL实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- 100行JS实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- 用Java开发贪吃蛇游戏
贪吃蛇游戏的设计步骤: Part 1: 设计游戏图纸 画出900*700的白色窗口 在窗口上添加画布 在画布上添加标题 在画布上添加黑色游戏区 Part 2: 放置静态的蛇:一个头.两个身体 加上开始 ...
- Qt 学习之路 2(34):贪吃蛇游戏(4)
Qt 学习之路 2(34):贪吃蛇游戏(4) 豆子 2012年12月30日 Qt 学习之路 2 73条评论 这将是我们这个稍大一些的示例程序的最后一部分.在本章中,我们将完成GameControlle ...
- Qt 学习之路 2(32):贪吃蛇游戏(2)
Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...
- 用C++实现的贪吃蛇游戏
我是一个C++初学者,控制台实现了一个贪吃蛇游戏. 代码如下: //"贪吃蛇游戏"V1.0 //李国良于2016年12月29日编写完成 #include <iostream& ...
- H5实现的可自定义贪吃蛇游戏
原创游戏,使用lufylegend.js开发 用canvas实现的贪吃蛇游戏,与一般的贪吃蛇游戏不同,图片经过美工设计,代码设计支持扩展和自定义. 游戏元素丰富,包括障碍物(仙人掌),金币(奖励),苹 ...
随机推荐
- App内测神器之蒲公英
一.前言部分 没使用蒲公英之前一直采用非常傻B的方式给公司App做内部测试,要么发个测试包让公司测试人员用iTUnes 自己安装 要么苦逼的一个个在我Xcode上直接安装测试包,操作起来又麻烦又苦逼, ...
- 移动端HTML
display:-webkit-box 把该元素中的所有元素变成块级元素,比如 <ul class="top-nav"> <li>地图</li> ...
- Maven实战系列文章
1.Maven命令行使用:mvn clean compile(编译) 2.Maven命令行使用:mvn clean package(打包) 3.Maven命令行使用:mvn clean install ...
- struts2杂记(一)——使用doubleSelect
一.前言 这段时间忙的要死,做项目,学框架,时间根本不够用,只能尽量抽出时间记录自己学过的东西. 1.1.doubleSelect 在之前学习中,我们使用过二级列表,相信很多人都理解其原理,在stru ...
- cookie入门
据我对cookie诞生背景的了解,cookie是由网景公司创建的,目的就是将用户的数据储存在客户端上.伴随的HTML5的出现,现在又有另外一个解决数据离线储存的方案,就是HTML5中的Web stor ...
- Ubantu【第一篇】:Ubantu中openssh连接
h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...
- SAP CRM 7.0中的BOL(Business Object Layer)
业务对象层(BOL)和通用交互层(GenIL)属于业务层. 业务对象层: 在CRM WebClient会话运行期间,业务对象层存储业务对象的数据以及它们属性和关系的定义. 通用交互层 通用交互层将 ...
- xamarin 一般错误解决办法
1. android_m2repository_r错误 问题描述: Unzipping failed. Please download https://dl-ssl.google.com/androi ...
- 【代码笔记】iOS-用户发布后能保存崩溃
一,工程图. 二,代码. AppDelegate.m #import "AppDelegate.h" #import "RootViewController.h" ...
- paramiko 的使用
paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于远程连接机器执行基本命令,也可以执行shell脚本 基于用户名密码连接: def ssh_connect ...