#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int,pair<int,int>>
using namespace std;
char arr[505][505];
//
map<char,int> dirx = {
{'N', -1},
{'E', 0},
{'S', 1},
{'W', 0},
};
map<char,int> diry = {
{'N', 0},
{'E', 1},
{'S', 0},
{'W', -1},
};
map <char,int> mp = {
{'N', 0},
{'E', 1},
{'S', 2},
{'W', 3},
};
vector<char> wheel(4);
bool vis[505][505];
signed main (){
wheel = {'N','E','S','W'};
int n,m; cin >> n >> m;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin >> arr[i][j];
}
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0,{0,0}});
while(!pq.empty()){
int d = pq.top().fi;
int x = pq.top().se.fi;
int y = pq.top().se.se;
vis[x][y] = true;
// cout << d << " " << x << " " << y << endl;
pq.pop();
if(x == m-1 && y == n-1){
cout << d << endl;
return 0;
}
if(arr[x][y] == 'X') continue;
for(auto &a:wheel){
int add = (mp[a] - mp[arr[x][y]] + 4)%4;
// cout << a << " " << add << endl;
if(x + dirx[a] < 0 || y + diry[a] < 0 || y + diry[a] >= n || x + dirx[a] >= m || vis[x + dirx[a]][y + diry[a]]) continue;
pq.push({d + add,{x + dirx[a],y + diry[a]}});
}
// cout << endl;
}
cout << "-1\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |