Advertisement
ccbeginner

UVa Q118

Feb 16th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. //UVa Q118
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct stat{
  6.     int x, y;
  7.     int dir;//0 := E, 1 := S, 2 := W, 3 := N
  8.     bool operator<(const stat &r)const{return (x == r.x)? y < r.y : x < r.x;}
  9. };
  10.  
  11. set<stat> U;
  12.  
  13. int n,m;
  14. char direction[] = "ESWN";
  15. int dx[] = {1, 0, -1, 0};
  16. int dy[] = {0, -1, 0, 1};
  17. bool lost = 0;
  18.  
  19. stat func(const stat &start, const string &s){
  20.     stat ret = start;
  21.     for(unsigned i = 0; i < s.size() && !lost; ++i){
  22.         //cout << ' ' << ret.x << ' ' << ret.y << ' ' << ret.dir << '\n';
  23.         if(s[i] == 'L')ret.dir = (ret.dir+3) % 4;
  24.         else if(s[i] == 'R')ret.dir = (ret.dir+1) % 4;
  25.         else{
  26.             int nx = ret.x+dx[ret.dir];
  27.             int ny = ret.y+dy[ret.dir];
  28.             if(0 <= nx && nx <= n && 0 <= ny && ny <= m){
  29.                 ret.x = nx;
  30.                 ret.y = ny;
  31.             }else{
  32.                 if(U.find(ret) == U.end())lost = 1;
  33.                 U.insert(ret);
  34.             }
  35.         }
  36.     }
  37.     return ret;
  38. }
  39.  
  40. int32_t main(){
  41.     cin >> n >> m;
  42.     stat start;
  43.     char c;
  44.     string s;
  45.     while(cin >> start.x >> start.y){
  46.         lost = 0;
  47.         cin >> c;
  48.         for(int i = 0; i < 4; ++i){
  49.             if(c == direction[i]){
  50.                 start.dir = i;
  51.                 break;
  52.             }
  53.         }
  54.         cin >> s;
  55.         stat ans = func(start, s);
  56.         cout << ans.x << ' ' << ans.y << ' ';
  57.         if(ans.dir == 0)cout << 'E';
  58.         else if(ans.dir == 1)cout << 'S';
  59.         else if(ans.dir == 2)cout << 'W';
  60.         else if(ans.dir == 3)cout << 'N';
  61.         if(lost)cout << " LOST";
  62.         cout << '\n';
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement