353. Design Snake Game
贪食蛇。
GAME OVER有2种情况,1是咬到自己,2是出界。
1)用QUEUE来保留占据的格子,每走一格就添加1个,然后POll()最后一个。 做一个一样的SET来check要走的格子是不是已经在自己身体里。这里需要注意用int[2]来加到SET里是不行的。。开始怀念C的指针了。 我们用一个比较典型的HASH来通过一个整数记录位置。
比如高是4,那么高的INDEX是0-3,不会超过4。对于一个点M,N来说,HASH最后的结果就是m*高 + n,这样不会有collision。。
2)出界比较好判断,知道当前要走的位置就行。。
GAME MOVE ON就是看吃豆(或者吃屎)的问题,用INDEX记录当前的豆,刷新的时候还要判断一次新豆在不在自己身体的SET里。
剩下的就是edge cases,比如要先POLL出再看能不能咬到自己;比如能吃的都吃了该怎么办,题里没说,但是看TEST CASE的意思是吃光了就不吃了。。但是游戏继续。
public class SnakeGame {
/** 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]. */
// boundry
int w;
int h;
// cur location
int m;
int n;
// food
int index;
int[][] f;
Set<Integer> set;
Queue<Integer> q;
public SnakeGame(int width, int height, int[][] food)
{
m = 0;
n = 0;
w = width;
h = height;
set = new HashSet<Integer>();
q = new LinkedList<Integer>();
set.add(0);
q.add(0);
f = food;
index = 0;
}
/** 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. */
public int move(String direction)
{
if((direction.equals("R") && n == w-1) || (direction.equals("L") && n == 0) ||
(direction.equals("U") && m == 0) || (direction.equals("D") && m == h-1)) return -1;
if(direction.equals("R"))
{
n++;
}
else if(direction.equals("L"))
{
n--;
}
else if(direction.equals("U"))
{
m--;
}
else if(direction.equals("D"))
{
m++;
}
if(index < f.length && m == f[index][0] && n == f[index][1])
{
index++;
while(index < f.length && eatself(f[index][0],f[index][1])) index++;
}
else
{
set.remove(q.poll());
if(eatself(m,n)) return -1;
}
int temp = m*w + n;
q.add(temp);
set.add(temp);
return set.size()-1;
}
public boolean eatself(int x, int y)
{
return set.contains(x*w+y);
}
}
/*
["SnakeGame","move","move","move","move","move","move"]
[[3,2,[[1,2],[0,1]]],["R"],["D"],["R"],["U"],["L"],["U"]]
["SnakeGame","move","move","move","move","move","move","move","move","move","move","move","move"]
[[3,3,[[2,0],[0,0],[0,2],[2,2]]],["D"],["D"],["R"],["U"],["U"],["L"],["D"],["R"],["R"],["U"],["L"],["D"]]
["SnakeGame","move","move"]
[[2,2,[[0,1]]],["R"],["D"]]
["SnakeGame","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move"]
[[3,3,[[2,0],[0,0],[0,2],[0,1],[2,2],[0,1]]],["D"],["D"],["R"],["U"],["U"],["L"],["D"],["R"],["R"],["U"],["L"],["L"],["D"],["R"],["U"]]
*/
/**
* Your SnakeGame object will be instantiated and called as such:
* SnakeGame obj = new SnakeGame(width, height, food);
* int param_1 = obj.move(direction);
*/
跌跌撞撞,错了好几次……考虑不周。
353. Design Snake Game的更多相关文章
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- Leetcode: Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [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 ...
- Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- 算法与数据结构基础 - 队列(Queue)
队列基础 队列具有“先进先出”的特点,用这个特点我们可以用它来处理时间序列相关或先后次序相关的问题,例如 LeetCode题目 933. Number of Recent Calls,时间复杂度O(1 ...
随机推荐
- C# 之【线程与进程】
1. 引言 先来个比喻手法: 如果把上课的过程比作进程,那么每个学生就是一个线程,他们共享教室,即线程共享进程的内存空间.每一个时刻,只能一个学生问老师问题,老师回答完毕,轮到下一个.即线程在一个时 ...
- 菜鸟日记之JSP1
JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它 是由Sun Microsyste ...
- 三种C#.net生成静态页面的方法
ASP.NET生成静态页面方法主要有三种 第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...
- GoogleCode新手教程
GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...
- 无Xaml的WPF展示
我们创建一个wpf应用程序,我们把里面的xaml文件全部删除,添加一个新类: 如下图: 然后我们cs文件中的代码: using System; using System.Collections.Gen ...
- css3实现无缝滚动效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 用C++实现绘制标尺的方法,使用了递归
在这个例子当中将使用递归来实现一个打印标尺刻度的方法.首先是递归,函数调用其本身就叫递归,在需要将一项工作不断分为两项较小的.类似的工作时,递归非常有用,递归的方法被称为分而治之策略. 下面是一个wi ...
- Java反射的理解
反射的作用: 1.运行时检查类的结构 2.运行时更改类的字段值 3.调用类的方法 准备知识: Class类:虚拟机为每一个对象保存的一份对象所属类的清单: static Class for ...
- Tomcat禁止显示目录和文件列表
Tomcat禁止显示目录和文件列表 打开 tomcat的安装目录/conf/web.xml 文件 <servlet> <servlet-name>default</s ...
- flat ui switch 改变状态而不响应事件
Flat UI是一套精美的扁平风格 UI 工具包,基于 Twitter Bootstrap实现.这套界面工具包含许多基本的和复杂的 UI 部件,例如按钮,输入框,组合按钮,复选框,单选按钮,标签,菜单 ...