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;
#define int long long
int h[200005];
struct g{int x, y, t;};
int df(char cur, char dir){
if(cur == dir) return 0;
if(cur == 'N') return 1 + df('E', dir);
if(cur == 'E') return 1 + df('S', dir);
if(cur == 'S') return 1 + df('W', dir);
if(cur == 'W') return 1 + df('N', dir);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<char>> a(n, vector<char>(m));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
}
vector<vector<int>> t(n, vector<int>(m, INT_MAX));
queue<g> bfs;
bfs.push({0, 0, 0});
while(!bfs.empty()){
g cur = bfs.front();
bfs.pop();
if(cur.x >= 0 && cur.x < n && cur.y >= 0 && cur.y < m && t[cur.x][cur.y] > cur.t){
t[cur.x][cur.y] = cur.t;
if(a[cur.x][cur.y] == 'X') continue;
bfs.push({cur.x-1, cur.y, cur.t + df(a[cur.x][cur.y], 'N')});
bfs.push({cur.x, cur.y+1, cur.t + df(a[cur.x][cur.y], 'E')});
bfs.push({cur.x+1, cur.y, cur.t + df(a[cur.x][cur.y], 'S')});
bfs.push({cur.x, cur.y-1, cur.t + df(a[cur.x][cur.y], 'W')});
}
}
if(t[n-1][m-1] == INT_MAX) cout << "-1\n";
else cout << t[n-1][m-1] << '\n';
return 0;
}
Compilation message (stderr)
adventure.cpp: In function 'long long int df(char, char)':
adventure.cpp:13:1: warning: control reaches end of non-void function [-Wreturn-type]
13 | }
| ^
# | 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... |