Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @author: WhiZTiM
- * @date: 5th May, 2014
- * @description: ACM-ICPC Regionals South Africa 2011
- */
- #include <iostream>
- #include <vector>
- #include <unordered_set>
- using namespace std;
- typedef std::vector<char> vchar;
- typedef std::vector<int> vint;
- struct Coord
- {
- int x; int y; int dist;
- bool operator == (const Coord lhs) const
- { return x == lhs.x && y == lhs.y; }
- };
- namespace std {
- template<>
- struct hash<Coord>
- {
- size_t operator() (const Coord coord) const
- { return hash<int>()(coord.x) + hash<int>()(coord.y); }
- };
- }
- void MoveToCordinate_inplace(char direction, Coord& coord)
- {
- coord.dist += 1;
- switch(direction)
- {
- case 'N':
- coord.y += 1;
- break;
- case 'E':
- coord.x += 1;
- break;
- case 'S':
- coord.y -= 1;
- break;
- case 'W':
- coord.x -= 1;
- break;
- default:
- //coord.dist -= 1;
- break;
- }
- }
- int GetShortestMoves(const vchar& inputs)
- {
- unordered_set<Coord> pathPoints;
- Coord prev = {0, 0, 0};
- pathPoints.insert(prev);
- for(auto ch : inputs)
- {
- MoveToCordinate_inplace(ch, prev);
- auto pIter = pathPoints.find(prev);
- if (pIter != pathPoints.end())
- prev.dist = pIter->dist;
- else
- pathPoints.insert(prev);
- }
- return prev.dist;
- }
- vchar GetNextInputSet(int moves)
- {
- vchar rtn(moves);
- for(int i = 0; i < moves; i++)
- cin >> rtn[i];
- return rtn;
- }
- int main()
- {
- std::ios::sync_with_stdio(false);
- int numPaths;
- cin >> numPaths;
- vint answers(numPaths);
- for(int i = 0; i < numPaths; i++) {
- int moves;
- cin >> moves;
- answers[i] = GetShortestMoves( GetNextInputSet(moves) );
- }
- for(auto i : answers)
- cout << i << "\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment