fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <utility>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. bool isPathCrossing(string s) {
  8. set <pair <int, int>> st;
  9. int x = 0, y = 0;
  10. st.insert(make_pair(x,y));
  11. for(int i = 0; i < s.size(); i++) {
  12. if(s[i] == 'N') y++;
  13. else if(s[i] == 'W') x--;
  14. else if(s[i] == 'S') y--;
  15. else x++;
  16. pair <int, int> pr = make_pair(x, y);
  17. if(st.find(pr) != st.end()) return true;
  18. st.insert(pr);
  19. }
  20. return false;
  21. }
  22.  
  23. int main() {
  24. cout << boolalpha;
  25. cout << isPathCrossing("NES") << endl;
  26. cout << isPathCrossing("NESWW") << endl;
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
false
true