贪食蛇。

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的更多相关文章

  1. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  2. Leetcode: Design Snake Game

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  3. [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 ...

  4. Design Snake Game

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

  8. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  9. 算法与数据结构基础 - 队列(Queue)

    队列基础 队列具有“先进先出”的特点,用这个特点我们可以用它来处理时间序列相关或先后次序相关的问题,例如 LeetCode题目 933. Number of Recent Calls,时间复杂度O(1 ...

随机推荐

  1. CSV文件规则

    CSV文件规则 1 开头是不留空,以行为单位.2 可含或不含列名,含列名则居文件第一行.3 一行数据不跨行,无空行.4 以半角逗号(即,)作分隔符,列为空也要表达其存在.5 列内容如存在半角逗号(即, ...

  2. 怎样在win7上远程连接linux系统

    window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤   在前面的环境和软件都有的情况下,双击putty.exe软件,如下图:   在软件界面中的:Hos ...

  3. C# 调用AForge类库操作摄像头

    如有雷同,不胜荣幸,若转载,请注明 最近做项目需要操作摄像头,在网上百度了很多资料,很多都是C#调用window API 发送SendMessage,实现操作摄像头,但是C#调用window API的 ...

  4. 关于Java中的选择排序法和冒泡排序法

    一,这种方法是直接传入一个数组进行排序(选择排序法) public static void selectSort(int arr[]){ for (int i = 0; i < arr.leng ...

  5. canvas之----浮动小球

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. ext 扩展控件—moneyField

    /** *数字控件 *带大写提示,和千分位 **/ Ext.define(appNameSpace+'.utils.MoneyField', { extend : 'Ext.form.field.Te ...

  7. PHP实战开发教程

    对于PHP初学者来说,一上手就学习庞大的PHP语法无疑很打击自信心.其实即便是很熟练的程序员,也未必对所有的语法非常熟悉.通常熟练的程序员比普通的程序员的优势在于对基本语法的理解非常透彻,而且常用的一 ...

  8. 将VIM配置成强大的IDE(二)

    将VIM配置成强大的IDE(二) 前面我们已经安装好了vundle这一款强大的插件管理工具. 下面,当然是配置我们需要的插件了. 在VIM下面通过命令 help vundle 我们可以知道,VUNDL ...

  9. Win7下通过easyBCD引导安装Ubuntu14.04

    Ubuntu14.04作为目前最新版本的ubuntu系统,相信很多人都想在自己的电脑上安装一下,然而系统的安装方法各式各样,u盘法.grub引导法等等,这里我将介绍在win7系统下用easyBCD软件 ...

  10. Android中通过typeface设置字体

    Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使用其他字体文件(*.ttf)方法一:XML中使用android默认字体 ...