Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UVa Q118
- #include <bits/stdc++.h>
- using namespace std;
- struct stat{
- int x, y;
- int dir;//0 := E, 1 := S, 2 := W, 3 := N
- bool operator<(const stat &r)const{return (x == r.x)? y < r.y : x < r.x;}
- };
- set<stat> U;
- int n,m;
- char direction[] = "ESWN";
- int dx[] = {1, 0, -1, 0};
- int dy[] = {0, -1, 0, 1};
- bool lost = 0;
- stat func(const stat &start, const string &s){
- stat ret = start;
- for(unsigned i = 0; i < s.size() && !lost; ++i){
- //cout << ' ' << ret.x << ' ' << ret.y << ' ' << ret.dir << '\n';
- if(s[i] == 'L')ret.dir = (ret.dir+3) % 4;
- else if(s[i] == 'R')ret.dir = (ret.dir+1) % 4;
- else{
- int nx = ret.x+dx[ret.dir];
- int ny = ret.y+dy[ret.dir];
- if(0 <= nx && nx <= n && 0 <= ny && ny <= m){
- ret.x = nx;
- ret.y = ny;
- }else{
- if(U.find(ret) == U.end())lost = 1;
- U.insert(ret);
- }
- }
- }
- return ret;
- }
- int32_t main(){
- cin >> n >> m;
- stat start;
- char c;
- string s;
- while(cin >> start.x >> start.y){
- lost = 0;
- cin >> c;
- for(int i = 0; i < 4; ++i){
- if(c == direction[i]){
- start.dir = i;
- break;
- }
- }
- cin >> s;
- stat ans = func(start, s);
- cout << ans.x << ' ' << ans.y << ' ';
- if(ans.dir == 0)cout << 'E';
- else if(ans.dir == 1)cout << 'S';
- else if(ans.dir == 2)cout << 'W';
- else if(ans.dir == 3)cout << 'N';
- if(lost)cout << " LOST";
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement