WhiZTiM

ACM-ICPC Regionals South Africa 2011 - erraticants

May 6th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. /*
  2.  *  @author: WhiZTiM
  3.  *  @date: 5th May, 2014
  4.  *  @description: ACM-ICPC Regionals South Africa 2011
  5.  */
  6. #include <iostream>
  7. #include <vector>
  8. #include <unordered_set>
  9. using namespace std;
  10. typedef std::vector<char> vchar;
  11. typedef std::vector<int> vint;
  12.  
  13. struct Coord
  14. {
  15.     int x; int y; int dist;
  16.     bool operator == (const Coord lhs) const
  17.     { return x == lhs.x && y == lhs.y; }
  18. };
  19.  
  20. namespace std {
  21.     template<>
  22.     struct hash<Coord>
  23.     {
  24.         size_t operator() (const Coord coord) const
  25.         {   return hash<int>()(coord.x) + hash<int>()(coord.y); }
  26.     };
  27. }
  28.  
  29. void MoveToCordinate_inplace(char direction, Coord& coord)
  30. {
  31.     coord.dist += 1;
  32.     switch(direction)
  33.     {
  34.         case 'N':
  35.             coord.y += 1;
  36.             break;
  37.         case 'E':
  38.             coord.x += 1;
  39.             break;
  40.         case 'S':
  41.             coord.y -= 1;
  42.             break;
  43.         case 'W':
  44.             coord.x -= 1;
  45.             break;
  46.         default:
  47.             //coord.dist -= 1;
  48.             break;
  49.     }
  50. }
  51.  
  52. int GetShortestMoves(const vchar& inputs)
  53. {
  54.     unordered_set<Coord> pathPoints;
  55.     Coord prev = {0, 0, 0};
  56.     pathPoints.insert(prev);
  57.    
  58.     for(auto ch : inputs)
  59.     {
  60.         MoveToCordinate_inplace(ch, prev);
  61.         auto pIter = pathPoints.find(prev);
  62.         if (pIter != pathPoints.end())
  63.             prev.dist = pIter->dist;
  64.         else
  65.             pathPoints.insert(prev);
  66.     }
  67.    
  68.     return prev.dist;
  69. }
  70.  
  71. vchar GetNextInputSet(int moves)
  72. {
  73.     vchar rtn(moves);
  74.     for(int i = 0; i < moves; i++)
  75.         cin >> rtn[i];
  76.     return rtn;
  77. }
  78.  
  79. int main()
  80. {
  81.     std::ios::sync_with_stdio(false);
  82.     int numPaths;
  83.     cin >> numPaths;
  84.     vint answers(numPaths);
  85.     for(int i = 0; i < numPaths; i++) {
  86.         int moves;
  87.         cin >> moves;
  88.         answers[i] = GetShortestMoves( GetNextInputSet(moves) );
  89.     }
  90.    
  91.     for(auto i : answers)
  92.         cout << i << "\n";
  93.    
  94.     return 0;
  95. }
Add Comment
Please, Sign In to add comment