This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n, m, ans = 1e9+7;
char g[505][505];
void bf(int x, int y, int putar){
// cout << x << ' ' << y << ' ' << putar << endl;
if(x == n && y == m){
ans = min(ans, putar);
}else{
if(g[x][y] == 'N'){
if(y < m) bf(x, y + 1, putar + 1);
if(x < n) bf(x + 1, y, putar + 2);
}else if(g[x][y] == 'E'){
if(y < m) bf(x, y + 1, putar);
if(x < n) bf(x + 1, y, putar + 1);
}else if(g[x][y] == 'S'){
if(y < m) bf(x, y + 1, putar + 3);
if(x < n) bf(x + 1, y, putar);
}else if(g[x][y] == 'W'){
if(y < m) bf(x, y + 1, putar + 2);
if(x < n) bf(x + 1, y, putar + 3);
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) cin >> g[i][j];
}
bf(1, 1, 0);
if(ans == 1e9+7) cout << -1;
else cout << ans;
}
/*
3 3
ENS
SXN
WSX
N atas
E kanan
S bawah
W kiri
N -> E
E -> S
S -> W
W -> N
4 3
NXE
SNX
XWN
NNX
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |