zoukankan      html  css  js  c++  java
  • LeetCode 353. Design Snake Game

    原题链接在这里:https://leetcode.com/problems/design-snake-game/

    题目:

    Design a Snake game that is played on a device with screen size = width x heightPlay 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)

    题解:

    The design question first needs to think what data structure is needed.

    We need to add to head and remove tail, thus we need Deque.

    We also need to maintain current body positions to check if it hit the body, thus we need a set.

    When calling move, we first get head position, update based on direction, check newHead is within boundary and not hitting the body, then add head to body.

    Then if new head is food, we don't need to remove tail. Otherwise, we need to remove tail.

    Time Complexity: SnakeGame, O(1). move, O(1).

    Space: O(height * width). body size.

    AC Java:

     1 class SnakeGame {
     2     LinkedList<Integer> que;
     3     HashSet<Integer> body;
     4     int m;
     5     int n;
     6     int [][] food;
     7     int index;
     8     int score;
     9     
    10     /** Initialize your data structure here.
    11         @param width - screen width
    12         @param height - screen height 
    13         @param food - A list of food positions
    14         E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
    15     public SnakeGame(int width, int height, int[][] food) {
    16         que = new LinkedList<>();
    17         que.offerFirst(0);
    18         body = new HashSet<>();
    19         body.add(0);
    20         
    21         m = height;
    22         n = width;
    23         this.food = food;
    24         index = 0;
    25         score = 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         if(score < 0){
    34             return -1;
    35         }
    36         
    37         LinkedList<Integer> localQue = que;
    38         HashSet<Integer> localBody = body;
    39         
    40         int head = que.peekFirst();
    41         int x = head / n;
    42         int y = head % n;
    43         switch(direction){
    44             case "U":
    45                 x--;
    46                 break;
    47             case "D":
    48                 x++;
    49                 break;
    50             case "L":
    51                 y--;
    52                 break;
    53             case "R":
    54                 y++;
    55                 break;
    56         }
    57         
    58         int newHead = x * n + y;
    59         
    60         // Remove tail position first since new head could be at old tail position
    61         body.remove(que.peekLast());
    62         if(x < 0 || x >= m || y < 0 || y >= n || body.contains(newHead)){
    63             score = -1;
    64             return score;
    65         }
    66         
    67         que.offerFirst(newHead);
    68         body.add(newHead);
    69         
    70         // If this is food, there is no need to remove tail.
    71         if(index < food.length && food[index][0] == x && food[index][1] == y){
    72             index++;
    73             score++;
    74             body.add(que.peekLast());
    75             return score;
    76         }
    77         
    78         // If this is not food, then we nned to remove tail.
    79         que.pollLast();
    80         return score;
    81     }
    82 }
    83 
    84 /**
    85  * Your SnakeGame object will be instantiated and called as such:
    86  * SnakeGame obj = new SnakeGame(width, height, food);
    87  * int param_1 = obj.move(direction);
    88  */
  • 相关阅读:
    解析URL
    文件上传
    MyEclipse自动生成hibernate实体类和配置文件攻略
    <form>表单提交时注意
    W2UI /W2Toolbar的click响应事件
    JS 读写文件
    select 美化(bootstrap)
    安装MySQL for Windows 数据库
    java环境配置—配置Tomcat8环境
    对进程、线程、应用程序域的理解
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12118260.html
Copyright © 2011-2022 走看看