First accepted solution


  • 0
    D
    public class SnakeGame {
    private LinkedList<Integer> snake;
    private int wid;
    private int hei;
    private int[][] m_food;
    private int idx;
    /** 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) {
        wid=width;
        hei=height;
        m_food=food;
        snake=new LinkedList();
        snake.add(0);
        snake.add(0);
        idx=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) {
        int w=snake.poll();
        int h=snake.peek();
        snake.addFirst(w);
        switch(direction){
            case "U":
                --w;
                break;
            case "L":
                --h;
                break;
            case "R":
                ++h;
                break;
            case "D":
                ++w;
                break;
            default:
                ++w;
        }
        if(w>=hei||w<0||h<0||h>=wid)return -1;
        if(idx<m_food.length && w==m_food[idx][0] && h==m_food[idx][1]){
            ++idx;
            snake.addFirst(h);
            snake.addFirst(w);
        }
        else{
            snake.pollLast();
            snake.pollLast();
            int k=snake.size();
            int p=0,a,b;
            while(p<k){
                a=snake.poll();
                b=snake.poll();
                snake.add(a);
                snake.add(b);
                ++p;
                ++p;
                if(a==w&&b==h)return -1;
            }
            snake.addFirst(h);
            snake.addFirst(w);
        }
        return idx;
    }
    

    }

    /**

    • Your SnakeGame object will be instantiated and called as such:
    • SnakeGame obj = new SnakeGame(width, height, food);
    • int param_1 = obj.move(direction);
      */

  • 1
    B

    I have a very similar idea. The main difference is I use a Pair class to keep track of the location which makes collision checking a bit cleaner.

    public class SnakeGame {
    private class Pair
    {
        int row;
        int col;
        public Pair(int row, int col)
        {
            this.row = row;
            this.col = col;
        }
    }
    
    private Queue<Pair> snake;
    private int[][] food;
    private int foodIndex;
    private int currentRow;
    private int currentCol;
    private int height;
    private int width;
    
    /** 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.height = height;
        this.width = width;
        this.food = food;
        foodIndex = 0;
        snake = new LinkedList<Pair>();
        snake.add(new Pair(0, 0));
    }
    
    private boolean collide(int row, int col)
    {
        for (Pair p: snake)
        {
            if (p.row == row && p.col == col) return true;
        }
        
        return false;
    }
    
    /** 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("U")) currentRow--;
        else if (direction.equals("D")) currentRow++;
        else if (direction.equals("L")) currentCol--;
        else currentCol++;
       
        if (foodIndex == food.length ||
            currentRow != food[foodIndex][0] || 
            currentCol != food[foodIndex][1])
        {
            snake.remove();
        }
        else
        {
            foodIndex++;
        }
        
         if (currentRow < 0 || currentCol < 0  || currentRow == height || currentCol == width || collide(currentRow, currentCol)) return -1;
        
        snake.add(new Pair(currentRow, currentCol));
        return snake.size() - 1;
    }
    

    }


Log in to reply
 

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