Submission #976259

#TimeUsernameProblemLanguageResultExecution timeMemory
976259vjudge1Awesome Arrowland Adventure (eJOI19_adventure)C++17
34 / 100
1 ms348 KiB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second 
int n, m, dp[505][505] = {0};
char g[505][505];

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++) g[i][0] = 'X';
    for(int i = 1; i <= m; i++) g[0][i] = 'X';
    
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> g[i][j];
            if(i == 1 && j == 1) continue;
            dp[i][j] = 1e9+7;
            
            if(g[i][j - 1] != 'X'){
                int tmp = dp[i][j - 1];
                if(g[i][j - 1] == 'N') tmp ++;
                else if(g[i][j - 1] == 'E') tmp += 0;
                else if(g[i][j - 1] == 'S') tmp += 3;
                else tmp += 2;
                dp[i][j] = min(dp[i][j], tmp);
            }
            if(g[i - 1][j] != 'X'){
                int tmp = dp[i - 1][j];
                if(g[i - 1][j] == 'N') tmp += 2;
                else if(g[i - 1][j] == 'E') tmp++;
                else if(g[i - 1][j] == 'S') tmp += 0;
                else tmp += 3;
                dp[i][j] = min(dp[i][j], tmp);
            }
        }
    } 
   
    if(dp[n][m] == 1e9+7) cout << -1;
    else cout << dp[n][m];
}
/*
N atas
E kanan
S bawah
W kiri

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...