贪食蛇。

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. C++文件操作详解(ifstream、ofstream、fstream)

    C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstre ...

  2. thinksns解析1

    1.数据库     这儿是关于数据库的封装,还是挺厉害的,最终select中完成sql语句的封装,最后由query来完成底层api     2.初始化过程 sns也是通过框架完成显示调用,一开始通过i ...

  3. 搭建laravel5全面教学,爬坑(windows下)。

    1.首先下载屌比的Composer 2.然后下载composer.phar 3.然后下载最新版Laravel框架 4.将下载下来的laravel压缩包扔到htdocs目录下(扔到别的目录没试过) 5. ...

  4. 【python之旅】python简介和入门

    python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...

  5. C# 数据结构 栈 Stack

    栈和队列是非常重要的两种数据结构,栈和队列也是线性结构,线性表.栈和队列这三种数据结构的数据元素和元素的逻辑关系也相同 差别在于:线性表的操作不受限制,栈和队列操作受限制(遵循一定的原则),因此栈和队 ...

  6. Scut 上线后遇到的问题

    1. 上线后的大并发问题: var sem = new Semaphore(_accepts, _accepts); while (true) { sem.WaitOne(); #pragma war ...

  7. JavaScript 语句后应该加分号么?

    分号加与不加完全取决于个人习惯,但为了代码稳定(解析出错)还是建议使用分号断句. JavaScript自动加分号规则:1.当有换行符(包括含有换行符的多行注释),并且下一个token没法跟前面的语法匹 ...

  8. jq实现瀑布流效果

    <!doctype html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. Rails中的测试RSpec升级遇到的问题

    bundle exec rspec spec/ /home/wuxj/Prac/rrprac/sample_app/spec/spec_helper.rb::in `block in <top ...

  10. spm使用之三spm应用实例

    spm 的init实际上是调用了grunt这个工具来实现一些交互式的提问和数据的获取. 看看npm就知道, npm有个命令叫init, 就是一样的交互式提问获取你要创建的nodejs的模块信息. sp ...