#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
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));
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') ok = false;
map[i][j] = trans(temp[j]);
}
}
if(ok) {
traverse(set<pair<int,int>>(), make_pair(0, 0), 0, map);
cout << res << "\n";
}
else cout << "-1\n";
}
Compilation message
adventure.cpp:27:11: error: 'INT_MAX' was not declared in this scope
27 | int res = INT_MAX;
| ^~~~~~~
adventure.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
3 | #include <set>
+++ |+#include <climits>
4 | #include <utility>