贪食蛇。

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. asp.net web api内部培训资料

    最近在公司进行了一次asp.net web api的分享吧,不算是培训. 可能大家有些人对Web API的推出目的还不是很了解,可以看看微软官方的定义,其实是为了提供一个好的REST Service方 ...

  2. SGU Volume 2

    SGU200.Cracking RSA------------------------★高斯消元 SGU207.Robbers -------------------------------数论 SG ...

  3. windows phone 之手势识别(Manipulation)

    在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...

  4. Java学习----Math函数

    public class TestMath { public static void main(String[] args) { System.out.println(Math.E); System. ...

  5. Centos 6.5 RedHat 6 安装mysql

    所需文件列表, 这些文件在安装光盘内的Packages文件夹内, 也可以到MySQL官方网站下载其他版本(需要FQ). 将这些文件放在/usr/loca/src文件夹: -.el6.x86_64.rp ...

  6. laravel学习:修改时区

    config/app.php 'timezone' => 'UTC', 改为 'timezone' => 'PRC',

  7. Android Camera拍照 压缩

    http://www.linuxidc.com/Linux/2014-12/110924.htm package com.klp.demo_025; import java.io.ByteArrayI ...

  8. strstr_while模型

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  9. iOS 宏定义_16进制色值转化为RGB返回UIColor类型对象

    // 十六进制颜色 #define COLOR_WITH_HEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) ...

  10. nodejs compressor

    http://www.2cto.com/kf/201203/122015.html http://www.cnblogs.com/terrylin/archive/2013/06/01/3112596 ...