-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path1296-C.cpp
50 lines (42 loc) · 1.09 KB
/
1296-C.cpp
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
/*
ad-hoc > implementation
difficulty: medium
date: 02/Mar/2020
hint: maps each position of the robot with the string index, and check if a new position has already been mapped before
by: @brpapa
*/
#include <iostream>
#include <map>
using namespace std;
#define INF (1 << 30)
map<pair<int, int>, int> grid;
int N; string str;
int l, r; // resposta
void walk(int i, int x, int y) {
// robô está na posição (x, y) e recebeu instrução p/ str[i]
if (grid.count(make_pair(x, y))) {
int nl = grid[make_pair(x, y)];
int nr = i-1;
if (nr-nl < r-l) {
r = nr;
l = nl;
}
}
grid[make_pair(x, y)] = i;
if (i == N) return;
else if (str[i] == 'L') walk(i+1, x-1, y);
else if (str[i] == 'R') walk(i+1, x+1, y);
else if (str[i] == 'U') walk(i+1, x, y+1);
else if (str[i] == 'D') walk(i+1, x, y-1);
}
int main() {
int T; cin >> T;
while (T--) {
grid.clear(); cin >> N >> str;
l = 0; r = INF;
walk(0, 0, 0);
if (r == INF) cout << -1 << endl;
else cout << l+1 << " " << r+1 << endl;
}
return 0;
}