Straightforward Java. Beats 99%+. Explained.


  • 0
    public class SnakeGame {
        int cols; 
        int rows; 
        int[][] food; 
        int foodI; 
        int score; 
        LinkedList<Point> body; 
        public SnakeGame(int width, int height, int[][] food) {
            this.cols = width;
            this.rows = height; 
            this.food = food; 
            this.score = 0;
            this.foodI = 0; 
            this.body = new LinkedList<Point>(); 
            body.add(new Point(0,0)); 
        }
        
        public int move(String direction) {
            char dir = direction.charAt(0);  
            Point head = body.peek(); 
            if(dir == 'U'){
                if(head.r-1<0) return -1; //boundary check
                if(foodI < food.length && food[foodI][0] == head.r-1 && food[foodI][1] == head.c){//found a food
                    foodI++; 
                    score++; 
                }else{//no food. remove a tail point. 
                    body.pollLast(); 
                }
                if(contains(head.r-1, head.c)) return -1; //check if it's going to hit itself
                body.addFirst(new Point(head.r-1, head.c)); //all clear, add the head. 
            }else if(dir == 'L'){
                if(head.c-1<0) return -1; 
                if(foodI < food.length && food[foodI][0] == head.r && food[foodI][1] == head.c-1){
                    foodI++; 
                    score++; 
                }else{
                    body.pollLast();
                }
                if(contains(head.r, head.c-1)) return -1; 
                body.addFirst(new Point(head.r, head.c-1)); 
            }else if(dir == 'R'){
                if(head.c+1>=cols) return -1; 
                if(foodI < food.length && food[foodI][0] == head.r && food[foodI][1] == head.c+1){
                    foodI++; 
                    score++; 
                }else{
                    body.pollLast();
                }
                if(contains(head.r, head.c+1)) return -1; 
                body.addFirst(new Point(head.r, head.c+1)); 
            }else if(dir == 'D'){
                if(head.r+1>=rows) return -1; 
                if(foodI < food.length && food[foodI][0] == head.r+1 && food[foodI][1] == head.c){
                    foodI++; 
                    score++; 
                }else{
                    body.pollLast(); 
                }
                if(contains(head.r+1, head.c)) return -1; 
                body.addFirst(new Point(head.r+1, head.c)); 
            }
            return score; 
        }
        
        private boolean contains(int r, int c){//check if hits itself
            for(Point pp: body){
                if(pp.r == r && c==pp.c) return true; 
            }
            return false; 
        }
        
        class Point{
            int r; 
            int c; 
            public Point(int r, int c){
                this.c = c;
                this.r = r; 
            }
        }
    }
    
    

    Use a FIFO queue (LinkedList). For each move, first check if it hits boundary, then check if we need to remove the tail point (which depends on the food[][], and we update score here), then check if it hits itself, lastly add the head.


Log in to reply
 

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