#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
using ll = long long;
using ld = long double;
void solve(int T);
void pre();
const int mxn = 1e5 + 5;
const int SQRT = 500;
const int LOG = 20;
const int inf = 1e18;
const int mod = 1e9 + 7;
const ld eps = 1e-9;
int32_t main(){
pre();
int tt = 1;
//cin>>tt;
for(int i = 1;i<=tt;i++)solve(i);
return 0;
}
void pre(){
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
}
int dir[4][2] = {
{0,-1}, //N
{1,0}, // E
{0,1},// S
{-1,0}// W
};
int val(char c){
if(c == 'N')return 0;
if(c == 'E')return 1;
if(c == 'S')return 2;
if(c == 'W')return 3;
return -1;
}
struct Node{
int x;
int y;
int cost;
};
bool operator<(Node a,Node b){
return a.cost > b.cost;
}
int n,m;
bool in(int x,int y){
return x >= 0 && y >= 0 && x<n && y < m;
}
void solve(int T){
cin>>m>>n;
char A[n][m];
bool vis[n][m];
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
char c;
cin>>c;
vis[i][j] = false;
A[i][j] = c;
}
}
priority_queue<Node> pq;
pq.push({0,0,0});
while(pq.size()){
Node t = pq.top();
pq.pop();
int x = t.x;
int y = t.y;
int cost = t.cost;
if(vis[x][y])continue;
vis[x][y] = true;
int arrow = val(A[x][y]);
cerr<<x<<" "<<y<<": "<<cost<<endl;
if(x == n-1 && y == m-1){
cout<<cost<<endl;
return;
}
if(arrow == -1)continue;
for(int i = 0;i<4;i++){
int d = (i + arrow)% 4;
int nx = x + dir[d][1];
int ny = y + dir[d][0];
if(!in(nx,ny))continue;
if(!vis[nx][ny])pq.push({nx,ny,cost + i});
}
}
cout<<-1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |