• 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)

    HashSet + Queue:

    吃东西的时候保留尾巴,不吃的时候删去尾巴

    one case to notice蛇转弯的时候,要先删去尾巴,再把新的点加进去。如果这个顺序不对的话,本来不会撞上尾巴的结果会判断会撞上

     1 public class SnakeGame {
     2     int m;
     3     int n;
     4     int[][] foods;
     5     Queue<Integer> queue;
     6     HashSet<Integer> set;
     7     int[] headPos;
     8     int foodSeq;
     9 
    10 
    11     /** Initialize your data structure here.
    12         @param width - screen width
    13         @param height - screen height 
    14         @param food - A list of food positions
    15         E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
    16     public SnakeGame(int width, int height, int[][] food) {
    17         this.m = height;
    18         this.n = width;
    19         this.foods = food;
    20         this.queue = new LinkedList<Integer>();
    21         this.set = new HashSet<Integer>();
    22         this.headPos = new int[]{0, 0};
    23         this.foodSeq = 0;
    24         queue.offer(0);
    25         set.add(0);
    26     }
    27     
    28     /** Moves the snake.
    29         @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 
    30         @return The game's score after the move. Return -1 if game over. 
    31         Game over when snake crosses the screen boundary or bites its body. */
    32     public int move(String direction) {
    33         int[] dir;
    34         switch(direction) {
    35             case "U": dir = new int[]{-1, 0}; break;
    36             case "L": dir = new int[]{0, -1}; break;
    37             case "R": dir = new int[]{0, 1}; break;
    38             case "D": dir = new int[]{1, 0}; break;
    39             default: dir = new int[2];
    40         }
    41         int row = headPos[0] + dir[0]; // new position row
    42         int col = headPos[1] + dir[1]; // new position col
    43         
    44         //delete tail first, before push into a new cell
    45         if (foodSeq<foods.length && calcPosId(foods[foodSeq][0], foods[foodSeq][1]) == calcPosId(row, col)) {
    46             foodSeq++;
    47         }
    48         else {
    49             set.remove(queue.poll());
    50         }
    51         
    52         if (row<0 || row>=m || col<0 || col>=n) return -1; // hit border
    53         if (set.contains(calcPosId(row, col))) return -1; // hit its own body
    54         
    55         set.add(calcPosId(row, col));
    56         queue.offer(calcPosId(row, col));
    57         headPos[0] = row;
    58         headPos[1] = col;
    59 
    60         return set.size()-1;
    61     }
    62     
    63     public int calcPosId(int x, int y) {
    64         return x*n+y;
    65     }
    66 }
    67 
    68 /**
    69  * Your SnakeGame object will be instantiated and called as such:
    70  * SnakeGame obj = new SnakeGame(width, height, food);
    71  * int param_1 = obj.move(direction);
    72  */

     如果没有39行,程序会说dir没有initialize,

    其实更好的写法应该是

     1         int rowHead = body.peekFirst() / width;
     2         int colHead = body.peekFirst() % width;
     3         switch (direction) {
     4             case "U" : rowHead--;
     5                        break;
     6             case "D" : rowHead++;
     7                        break;
     8             case "L" : colHead--;
     9                        break;
    10             default :  colHead++;
    11         }
  • 相关阅读:
    PHP中利用jQuery操作json格式数据,实现$_POST的数据传输和接收
    如何快速掌握一门技术【婴儿最强学习回头看一看】
    显示桌面.scf
    注册表数据库
    win10home_fixgpedit.msc
    Eclipse 中 jetty 调试模式(debug)正常启动无法访问;非调试模式正常
    svn中的与资源库同步操作 讲解
    windows下二进制mysql的卸载以及安装教程
    mysql服务正在启动或停止中请稍后片刻再试一次,服务强制杀死的方法
    Eclipse中git检出、更新、提交、合并分支、以及解决冲突
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6185503.html
Copyright © 2020-2023  润新知