#include <bits/stdc++.h>
#include <chrono>
#define ll long long
#define ull unsigned long long
#define ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf 1e15
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
using namespace std;
using namespace std::chrono;
/*auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " microseconds" << endl;*/
const int maxn = 505;
int maxi = 500*500+1;
string a[maxn];
int n,m;
map<char,map<char,int> > mp;
int calc(int i,int j,int c,int bio[maxn][maxn]){
if((i==n-1&&j==n-1)==true) return c;
if(a[i][j]=='X') return maxi;
int x=maxi,y=maxi,z=maxi,w=maxi;
if(i<n-1&&bio[i+1][j]==0){
bio[i+1][j]=1;
if(a[i][j]!='S'||a[i][j]=='X') x = calc(i+1,j,c+mp[a[i][j]]['S'],bio);
else x = calc(i+1,j,c,bio);
bio[i+1][j]=0;
}
if(i>0&&bio[i-1][j]==0){
bio[i-1][j]=1;
if(a[i][j]!='N'||a[i][j]=='X') y = calc(i-1,j,c+mp[a[i][j]]['N'],bio);
else y =calc(i-1,j,c,bio);
bio[i-1][j]=0;
}
if(j>0&&bio[i][j-1]==0){
bio[i][j-1]=1;
if(a[i][j]!='W'||a[i][j]=='X') z = calc(i,j-1,c+mp[a[i][j]]['W'],bio);
else z = calc(i,j-1,c,bio);
bio[i][j-1]=0;
}
if(j<m-1&&bio[i][j+1]==0){
bio[i][j+1]=1;
if(a[i][j]!='E'||a[i][j]=='X') w = calc(i,j+1,c+mp[a[i][j]]['E'],bio);
else w = calc(i,j+1,c,bio);
bio[i][j+1]=0;
}
return min(x,min(y,min(z,w)));
}
int main(){
cin >> n>> m;
int k[maxn][maxn];
for(int i = 0;i<n;i++){
cin >> a[i];
}
mp['N']['E']=1;
mp['N']['S']=2;
mp['N']['W']=3;
mp['E']['S']=1;
mp['E']['W']=2;
mp['E']['N']=3;
mp['S']['W']=1;
mp['S']['N']=2;
mp['S']['E']=3;
mp['W']['N']=1;
mp['W']['E']=2;
mp['W']['S']=3;
if(n==1){
string s = a[0];
int ans = 0;
for(int i = 0;i<m-1;i++){
if(s[i]=='X') {
cout<<"-1";
goto Next;
}
if(s[i]!='E'){
ans+=4-mp['E'][s[i]];
}
}
cout<<ans;
Next:;
}else{
int ans = calc(0,0,0,k);
if(ans==maxi) printf("-1");
else printf("%d",ans);
}
}
Compilation message
adventure.cpp:5:9: warning: ISO C++11 requires whitespace after the macro name
5 | #define ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |