#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,id[509][509],bo[500009];
string ss[509];
vector <pair <int, int> > v[500009];
map <char, int> m;
priority_queue <pair <int, int> > s;
void chk(int q, int w){
if(q<0||q>a||w<0||w>b) return;
int qw=0,we=0;
if(q==i-1) we=1;
if(w==j+1) we=2;
if(q==i+1) we=3;
if(w==j-1) we=4;
if(we>=m[ss[i][j]]){
qw=we-m[ss[i][j]];
}else{
qw=4+we-m[ss[i][j]];
}
v[id[i][j]].push_back(make_pair(id[q][w],qw));
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(i=1; i<=a; i++){
cin>>ss[i];
ss[i].insert(0,"0");
}
m['N']=1;m['E']=2;m['S']=3;m['W']=4;
for(i=1; i<=a; i++) for(j=1; j<=b; j++){
id[i][j]=i*(b+2)+j;
/*pr[id[i][j]].first=i;
pr[id[i][j]].second=j;*/
}
for(i=1; i<=a; i++){
for(j=1; j<=b; j++){
if(ss[i][j]=='X') continue;
chk(i-1,j);
chk(i+1,j);
chk(i,j-1);
chk(i,j+1);
}
}
s.push(make_pair(0,id[1][1]));
zx=-1;
while(s.size()){
while(s.size()&&bo[s.top().second]==1) s.pop();
if(s.size()==0) break;
c=s.top().second;d=s.top().first;
//cout<<pr[c].first<<" "<<pr[c].second<<" "<<d<<endl;
if(c==id[a][b]){
zx=-d;
break;
}
s.pop();
bo[c]=1;
for(vector <pair <int, int> >::iterator it=v[c].begin(); it!=v[c].end(); it++){
if(bo[(*it).first]==1) continue;
s.push(make_pair(d-(*it).second,(*it).first));
}
}
cout<<zx;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12288 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12048 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12276 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12288 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12048 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12276 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
9 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
10 ms |
12160 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12160 KB |
Output is correct |
27 |
Correct |
9 ms |
12160 KB |
Output is correct |
28 |
Correct |
9 ms |
12160 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12160 KB |
Output is correct |
31 |
Correct |
9 ms |
12160 KB |
Output is correct |
32 |
Correct |
8 ms |
12160 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
8 ms |
12160 KB |
Output is correct |
35 |
Correct |
19 ms |
13184 KB |
Output is correct |
36 |
Correct |
9 ms |
12160 KB |
Output is correct |
37 |
Correct |
20 ms |
13552 KB |
Output is correct |
38 |
Correct |
21 ms |
14336 KB |
Output is correct |
39 |
Correct |
131 ms |
24300 KB |
Output is correct |
40 |
Correct |
128 ms |
24444 KB |
Output is correct |
41 |
Correct |
78 ms |
23160 KB |
Output is correct |
42 |
Correct |
128 ms |
24440 KB |
Output is correct |
43 |
Correct |
133 ms |
28524 KB |
Output is correct |
44 |
Correct |
74 ms |
23368 KB |
Output is correct |