When I revisited this problem, I missed line 35. It's important to check the index before accessing an array.
1: class SnakeGame { 2: private: 3: int w, h, i; 4: deque<pair<int, int>> q; 5: vector<pair<int, int>> f; 6: set<pair<int, int>> s; 7: public: 8: /** Initialize your data structure here. 9: @param width - screen width 10: @param height - screen height 11: @param food - A list of food positions 12: E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */ 13: SnakeGame(int width, int height, vector<pair<int, int>> food) { 14: w = width, h = height, i = 0; 15: f = food; 16: q.push_back(make_pair(0, 0)); 17: } 18: /** Moves the snake. 19: @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 20: @return The game's score after the move. Return -1 if game over. 21: Game over when snake crosses the screen boundary or bites its body. */ 22: int move(string direction) { 23: pair<int, int> head = q.front(); 24: pair<int, int> tail = q.back(); 25: int r = head.first, c = head.second; 26:
q.pop_back();
27:
s.erase(tail);
28: if (direction == "U") r--; 29: else if (direction == "D") r++; 30: else if (direction == "R") c++; 31: else if (direction == "L") c--; 32: if (r < 0 || r == h || c < 0 || c == w || s.count(make_pair(r, c))) return -1; 33: q.push_front(make_pair(r, c)); 34: s.insert(make_pair(r, c)); 35:
if (i == f.size()) return q.size()-1;
36: if (f[i].first == r && f[i].second == c) { 37: q.push_back(tail); 38: s.insert(make_pair(tail.first, tail.second)); 39: i++; 40: } 41: return q.size()-1; 42: } 43: }; 44: /** 45: * Your SnakeGame object will be instantiated and called as such: 46: * SnakeGame obj = new SnakeGame(width, height, food); 47: * int param_1 = obj.move(direction); 48: */
No comments:
Post a Comment