1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr) using namespace std;
int b4i[] = {0, 1, 0, -1}, b4j[] = {1, 0, -1, 0}; bool vis[100][100]; map<char, int> mp; char mp_inv[4]; int main(){ fastio; mp['E'] = 0, mp['N'] = 1, mp['W'] = 2, mp['S'] = 3; mp_inv[0] = 'E', mp_inv[1] = 'N', mp_inv[2] = 'W', mp_inv[3] = 'S'; int m, n, x, y, dir; char pos; string order; bool suc; cin >> n >> m; while(cin >> x){ cin >> y >> pos; cin >> order; dir = mp[pos]; suc = true; for(int i = 0; i < order.length(); ++i){ if(order[i] == 'L'){ ++dir; if(dir > 3) dir = 0; } else if(order[i] == 'R'){ --dir; if(dir < 0) dir = 3; } else { int di = y + b4i[dir], dj = x + b4j[dir]; if((di > m || dj > n || di < 0 || dj < 0) && !vis[y][x]){ vis[y][x] = true; suc = false; break; } else if(di <= m && dj <= n && di >= 0 && dj >= 0){ y = di, x = dj; } } } cout << x << " " << y << " " << mp_inv[dir]; if(!suc) cout << " LOST"; cout << "\n"; } }
|