#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
#include <climits>
using namespace std;
int trans(char ch) {
switch(ch) {
case 'N':
return 0;
case 'E':
return 1;
case 'S':
return 2;
case 'W':
return 3;
default:
return -1;
}
}
int dis(int from, int to) {
if(from <= to) return to-from;
else return 4-from+to;
}
int res = INT_MAX;
int n, m;
bool ok = true;
void traverse(set<pair<int,int>> visited, pair<int,int> loc, int ans, vector<vector<int>>& map) {
if(loc.first == n-1 && loc.second == m-1) {
res = min(res, ans);
return;
}
visited.insert(loc);
for(int i = 0; i < 4; i++) {
pair<int,int> nu = loc;
switch(i) {
case 0:
nu.first--;
break;
case 1:
nu.second++;
break;
case 2:
nu.first++;
break;
case 3:
nu.second--;
break;
}
if(nu.first > -1 && nu.second > -1 && nu.first < n && nu.second < m && visited.find(nu) == visited.end()) traverse(visited, nu, ans+dis(map[loc.first][loc.second], i), map);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
string temp;
vector<vector<int>> map(n, vector<int>(m));
set<pair<int,int>> visited;
for(int i = 0; i < n; i++) {
cin >> temp;
for(int j = 0; j < m; j++) {
if(i == n-1 && j == m-1) continue;
if(temp[j] == 'X') visited.insert(make_pair(i, j));
map[i][j] = trans(temp[j]);
}
}
if(visited.find(make_pair(0,0)) == visited.end()) traverse(visited, make_pair(0, 0), 0, map);
if(res == INT_MAX) cout << "-1\n";
else cout << res << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2132 KB |
Output is correct |
4 |
Correct |
6 ms |
4436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1060 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2132 KB |
Output is correct |
4 |
Correct |
6 ms |
4436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1060 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
9 ms |
5972 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
4 ms |
3412 KB |
Output is correct |
17 |
Correct |
5 ms |
3668 KB |
Output is correct |
18 |
Correct |
1 ms |
540 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
7 ms |
5588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Execution timed out |
2065 ms |
596 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2132 KB |
Output is correct |
4 |
Correct |
6 ms |
4436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1060 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
9 ms |
5972 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
4 ms |
3412 KB |
Output is correct |
17 |
Correct |
5 ms |
3668 KB |
Output is correct |
18 |
Correct |
1 ms |
540 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
7 ms |
5588 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Execution timed out |
2065 ms |
596 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |