#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int ll
const ll mod=1e9+7;
int n,m;
char c[501][501];
bool check(int x, int y){
if(x<1 || y<1 || x>n || y>m ) return false;
if(x==n && y==m) return true;
if(c[x][y]=='X') return false;
return true;
}
main(){
cin>>n>>m;
vector< pair<int,int> > v[n*m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c[i][j];
}
}
vector<pair<int,int> > p={{-1,0},{0,1},{1,0},{0,-1}};
vector<int> t={-m,1,m,-1};
map<char,int> mp;
mp['N']=0;
mp['E']=1;
mp['S']=2;
mp['W']=3;
int x=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
x++;
if(c[i][j]=='X') continue;
int l=mp[c[i][j]];
for(int k=0;k<4;k++){
int a=i+p[k].first;
int b=j+p[k].second;
bool ok = check(a,b);
if(ok==false) continue;
int dist;
if(k<l) dist=(k+4)-l;
else dist=k-l;
v[x].pb({x+t[k],dist});
}
}
}
// for(int i=1;i<=n*m;i++){
// for(int j=0;j<v[i].size();j++){
// cout<<i<<" "<<v[i][j].first<<" "<<v[i][j].second<<endl;
// }
// }
vector<int> d(n*m+1,INT_MAX);
d[1]=0;
set< pair<int,int> > q;
q.insert({0,1});
while(!q.empty()){
//pair<int,int> pr=q.begin();
int u=q.begin()->second;
q.erase(q.begin());
for(auto edge : v[u]){
int to = edge.first;
int len = edge.second;
if(d[u] + len < d[to]){
q.erase({d[to],to});
d[to]=d[u]+len;
q.insert({d[to],to});
}
}
}
// for(int i=1;i<=n*m;i++) cout<<i<<" "<<d[i]<<endl;
cout<<d[n*m]<<endl;
}
Compilation message
adventure.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |