# What's the trick of this problem?

• Why is my solution wrong? I think this problem is easy until I realize that I may have missed something or didn't understand the problem.

Thank you !

``````class SnakeGame {
int score, x, y, w,h;
queue<pair<int,int>> q;
public:
/** 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]. */
SnakeGame(int width, int height, vector<pair<int, int>> food) {
x=0;
y=0;
score=0;
w=width;
h=height;

for (auto & f:food)
{
q.push(f);
}
}

/** 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. */
int move(string direction) {
if (direction=="U")
x--;
else if (direction=="L")
y--;
else if (direction== "R")
y++;
else if (direction=="D")
x++;

if (x<0||y<0||x>=h||y>=w)
return -1;
if (q.empty())
return score;
if (x==q.front().first&&y==q.front().second)
{
score++;
q.pop();
}

return score;
}
};

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

• Snake may collied with itself and Game Over

• The important rule of this game is that the snake will grow longer with one unit after eating a food, so you need to track the position of entire snake. Also, as it grows, it will be more likely to collide with itself to die. Your code assumes the snake never grows.

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