Java easy solution


  • 0
    W
    public class SnakeGame {
        class Node {
            int x;
            int y;
            
            public Node(int x, int y) {
                this.x = x;
                this.y = y;
            }
            
            @Override
            public boolean equals(Object obj) {
                if (obj == null) {
                    return false;
                }
                if (!Node.class.isAssignableFrom(obj.getClass())) {
                    return false;
                }
                final Node other = (Node) obj;
                if (this.x != other.x || this.y != other.y) return false;
                return true;
            }
            
            @Override
            public int hashCode() {
                int hash = 3;
                hash = 53 * hash + this.x;
                hash = 53 * hash + this.y;
                return hash;
            }
        }
        
        private List<Node> snake;
        private Set<Node> set;
        private int[][] food;
        private int curFoodPos;
        private int M, N;
    
        /** 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]. */
        public SnakeGame(int width, int height, int[][] food) {
            this.snake = new ArrayList<>();
            this.food = food;
            this.curFoodPos = 0;
            this.set = new HashSet<>();
            this.M = height;
            this.N = width;
            
            //Initialization
            Node origin = new Node(0, 0);
            set.add(origin);
            snake.add(origin);
        }
        
        /** 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) {
            //Get the head of the "snake"
            Node preHead = snake.get(0);
    
            Node head = new Node(preHead.x, preHead.y);
            
            //Move the head
            if ("U".equals(direction)) {
                head.x--;
            } else if ("D".equals(direction)) {
                head.x++;
            } else if ("L".equals(direction)) {
                head.y--;
            } else {
                head.y++;
            }
    
            //If the head eat the food, update the index of current food, otherwise remove the tail
            if (curFoodPos < food.length && head.x == food[curFoodPos][0] && head.y == food[curFoodPos][1]) {
                curFoodPos++;
            } else {
                Node tail = snake.get(snake.size() - 1);
                set.remove(tail);
                snake.remove(snake.size() - 1);
            }
            
            //If the head is collides with border or its body, return -1, this check sentence should be placed after removing tail
            if (head.x < 0 || head.x >= M || head.y < 0 || head.y >= N || set.contains(head)) return -1;
            
            //Add the new head to the list and set
            snake.add(0, head);
            set.add(head);
    
            return snake.size() - 1;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.