1496.cpp (647B)
1 struct Hash { 2 size_t operator()(const tuple<int, int> &t) const { return (get<0>(t) << 16) + get<1>(t); } 3 }; 4 5 class Solution { 6 public: 7 bool isPathCrossing(const string &path) const { 8 static const pair<int, int> moves[128] = { 9 ['N'] = {-1, 0}, ['S'] = {1, 0}, ['E'] = {0, 1}, ['W'] = {0, -1}}; 10 unordered_set<tuple<int, int>, Hash> us; 11 int x = 0, y = 0; 12 us.insert({0, 0}); 13 for (const char c : path) { 14 x += moves[c].first; 15 y += moves[c].second; 16 if (us.count({x, y})) return true; 17 us.insert({x, y}); 18 } 19 return false; 20 } 21 };