-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva11831.cpp
More file actions
105 lines (97 loc) · 2.92 KB
/
uva11831.cpp
File metadata and controls
105 lines (97 loc) · 2.92 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Rishikesh
* UVA 11831 Sticker Collector Robot
*/
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<iomanip>
#include<cstdint>
#include<numeric>
#include<stack>
#include<queue>
#include<list>
#include<deque>
#include<bitset>
#include<regex>
using namespace std;
char directions[]{'N', 'L', 'S', 'O'};
map<char, int> dirindex{{'N', 0}, {'L', 1}, {'S', 2}, {'O', 3}};
void updateAnswerArena(int &ans, vector<string> &arena, int x, int y) {
if (arena[y][x] == '*') {
ans++;
arena[y][x] = '.';
}
}
int main() {
string str;
int n,m,s;
while(getline(cin, str), str[0] != '0') {
istringstream iss(str);
iss >> n >> m >> s;
vector<string> arena(n);
for(int i = 0; i < n; i++) {
getline(cin, arena[i]);
}
string instructions;
getline(cin, instructions);
int x,y=-1;
char orientation;
{
regex initloc("(N|S|O|L)");
smatch sm;
for (y = 0; y < n; y++) {
if (regex_search(arena[y],sm,initloc)) {
x = sm.position();
orientation = arena[y][x];
break;
}
}
}
int ans = 0;
for (int i = 0; i < instructions.length(); i++) {
switch(instructions[i]) {
case 'D':
orientation = directions[(dirindex[orientation] + 1) % 4];
break;
case 'E':
orientation = directions[(dirindex[orientation] + 3) % 4];
break;
case 'F':
switch(orientation) {
case 'N':
if (y != 0 and arena[y - 1][x] != '#') {
y--;
updateAnswerArena(ans, arena, x, y);
}
break;
case 'S':
if (y != n - 1 and arena[y + 1][x] != '#') {
y++;
updateAnswerArena(ans, arena, x, y);
}
break;
case 'O':
if (x != 0 and arena[y][x - 1] != '#') {
x--;
updateAnswerArena(ans, arena, x, y);
}
break;
case 'L':
if (x != m - 1 and arena[y][x+1] != '#') {
x++;
updateAnswerArena(ans, arena, x, y);
}
break;
}
break;
}
}
cout << ans << endl;
}
return 0;
}