#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vll vector<long long>
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
void print(vi v){
cout<<"Contents of vector:\n";
for(auto x : v) cout<<x<<" ";
cout<<endl<<endl;
}
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
vector<pair<int, int> > adj[509 * 509];
int m, n;
char ar[509][509];
int dists[509 * 509];
bool in(int i, int j){
return (i >= 0) && (j >= 0) && (i<m) && (j<n);
}
void fillAdj(){
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(ar[i][j] == 'E'){
for(int k = 0; k<4; k++){
int ci = i + dy[k], cj = j + dx[k];
if(in(ci, cj)){
if(k == 0) { /// Going W
adj[i*n + j].push_back({ci*n + cj, 2});
}
if(k == 1) { /// Going N
adj[i*n + j].push_back({ci*n + cj, 3});
}
if(k == 2) { /// Going E
adj[i*n + j].push_back({ci*n + cj, 0});
}
if(k == 3) { /// Going S
adj[i*n + j].push_back({ci*n + cj, 1});
}
}
}
}
if(ar[i][j] == 'W'){
for(int k = 0; k<4; k++){
int ci = i + dy[k], cj = j + dx[k];
if(in(ci, cj)){
if(k == 0) { /// Going W
adj[i*n + j].push_back({ci*n + cj, 0});
}
if(k == 1) { /// Going N
adj[i*n + j].push_back({ci*n + cj, 1});
}
if(k == 2) { /// Going E
adj[i*n + j].push_back({ci*n + cj, 2});
}
if(k == 3) { /// Going S
adj[i*n + j].push_back({ci*n + cj, 3});
}
}
}
}
if(ar[i][j] == 'S'){
for(int k = 0; k<4; k++){
int ci = i + dy[k], cj = j + dx[k];
if(in(ci, cj)){
if(k == 0) { /// Going W
adj[i*n + j].push_back({ci*n + cj, 1});
}
if(k == 1) { /// Going N
adj[i*n + j].push_back({ci*n + cj, 2});
}
if(k == 2) { /// Going E
adj[i*n + j].push_back({ci*n + cj, 3});
}
if(k == 3) { /// Going S
adj[i*n + j].push_back({ci*n + cj, 0});
}
}
}
}
if(ar[i][j] == 'N'){
for(int k = 0; k<4; k++){
int ci = i + dy[k], cj = j + dx[k];
if(in(ci, cj)){
if(k == 0) { /// Going W
adj[i*n + j].push_back({ci*n + cj, 3});
}
if(k == 1) { /// Going N
adj[i*n + j].push_back({ci*n + cj, 0});
}
if(k == 2) { /// Going E
adj[i*n + j].push_back({ci*n + cj, 1});
}
if(k == 3) { /// Going S
adj[i*n + j].push_back({ci*n + cj, 2});
}
}
}
}
}
}
}
void dijkstra(int source){
bool visited[n*m];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++) dists[i*n + j] = INT_MAX;
}
for(int i = 0; i<m; i++) for(int j = 0; j<n; j++) visited[i*n + j] = false;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
pq.push({0, source});
dists[source] = 0;
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
if (visited[u]) continue;
else visited[u] = true;
//cout<<"From "<<u<<":"<<endl;
for(auto v : adj[u]) {
//cout<<v.first<<endl;
if (dists[v.first] > dists[u] + v.second) {
dists[v.first] = dists[u] + v.second;
pq.push({dists[v.first], v.first});
}
}
//cout<<endl;
}
}
void solve(){
cin>>m>>n;
for(int i = 0; i<m; i++) for(int j = 0; j<n; j++) cin>>ar[i][j];
fillAdj();
dijkstra(0);
/*for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
cout<<dists[i*n + j]<<" ";
}
cout<<endl;
}*/
if(dists[m*n - 1] == INT_MAX) cout<<"-1"<<endl;
else cout<<dists[m*n - 1]<<endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
//ifstream cin(""); ofstream cout("");///cia failai
//int T; cin>>T;
int T = 1;
for(int it = 1; it<=T; it++){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6400 KB |
Output is correct |
2 |
Correct |
4 ms |
6400 KB |
Output is correct |
3 |
Correct |
5 ms |
6528 KB |
Output is correct |
4 |
Correct |
5 ms |
6400 KB |
Output is correct |
5 |
Correct |
5 ms |
6400 KB |
Output is correct |
6 |
Correct |
5 ms |
6400 KB |
Output is correct |
7 |
Correct |
5 ms |
6400 KB |
Output is correct |
8 |
Correct |
5 ms |
6400 KB |
Output is correct |
9 |
Correct |
5 ms |
6400 KB |
Output is correct |
10 |
Correct |
5 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6400 KB |
Output is correct |
2 |
Correct |
4 ms |
6400 KB |
Output is correct |
3 |
Correct |
5 ms |
6528 KB |
Output is correct |
4 |
Correct |
5 ms |
6400 KB |
Output is correct |
5 |
Correct |
5 ms |
6400 KB |
Output is correct |
6 |
Correct |
5 ms |
6400 KB |
Output is correct |
7 |
Correct |
5 ms |
6400 KB |
Output is correct |
8 |
Correct |
5 ms |
6400 KB |
Output is correct |
9 |
Correct |
5 ms |
6400 KB |
Output is correct |
10 |
Correct |
5 ms |
6400 KB |
Output is correct |
11 |
Correct |
4 ms |
6400 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6400 KB |
Output is correct |
14 |
Correct |
5 ms |
6400 KB |
Output is correct |
15 |
Correct |
4 ms |
6400 KB |
Output is correct |
16 |
Correct |
4 ms |
6400 KB |
Output is correct |
17 |
Correct |
4 ms |
6400 KB |
Output is correct |
18 |
Correct |
4 ms |
6400 KB |
Output is correct |
19 |
Correct |
5 ms |
6400 KB |
Output is correct |
20 |
Correct |
6 ms |
6528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6400 KB |
Output is correct |
2 |
Correct |
5 ms |
6400 KB |
Output is correct |
3 |
Correct |
5 ms |
6400 KB |
Output is correct |
4 |
Correct |
5 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
Output is correct |
2 |
Correct |
4 ms |
6400 KB |
Output is correct |
3 |
Correct |
5 ms |
6400 KB |
Output is correct |
4 |
Correct |
5 ms |
6400 KB |
Output is correct |
5 |
Correct |
5 ms |
6528 KB |
Output is correct |
6 |
Correct |
5 ms |
6528 KB |
Output is correct |
7 |
Correct |
5 ms |
6400 KB |
Output is correct |
8 |
Correct |
5 ms |
6400 KB |
Output is correct |
9 |
Correct |
5 ms |
6400 KB |
Output is correct |
10 |
Correct |
5 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6400 KB |
Output is correct |
2 |
Correct |
4 ms |
6400 KB |
Output is correct |
3 |
Correct |
5 ms |
6528 KB |
Output is correct |
4 |
Correct |
5 ms |
6400 KB |
Output is correct |
5 |
Correct |
5 ms |
6400 KB |
Output is correct |
6 |
Correct |
5 ms |
6400 KB |
Output is correct |
7 |
Correct |
5 ms |
6400 KB |
Output is correct |
8 |
Correct |
5 ms |
6400 KB |
Output is correct |
9 |
Correct |
5 ms |
6400 KB |
Output is correct |
10 |
Correct |
5 ms |
6400 KB |
Output is correct |
11 |
Correct |
4 ms |
6400 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6400 KB |
Output is correct |
14 |
Correct |
5 ms |
6400 KB |
Output is correct |
15 |
Correct |
4 ms |
6400 KB |
Output is correct |
16 |
Correct |
4 ms |
6400 KB |
Output is correct |
17 |
Correct |
4 ms |
6400 KB |
Output is correct |
18 |
Correct |
4 ms |
6400 KB |
Output is correct |
19 |
Correct |
5 ms |
6400 KB |
Output is correct |
20 |
Correct |
6 ms |
6528 KB |
Output is correct |
21 |
Correct |
5 ms |
6400 KB |
Output is correct |
22 |
Correct |
5 ms |
6400 KB |
Output is correct |
23 |
Correct |
5 ms |
6400 KB |
Output is correct |
24 |
Correct |
5 ms |
6400 KB |
Output is correct |
25 |
Correct |
5 ms |
6528 KB |
Output is correct |
26 |
Correct |
4 ms |
6400 KB |
Output is correct |
27 |
Correct |
5 ms |
6400 KB |
Output is correct |
28 |
Correct |
5 ms |
6400 KB |
Output is correct |
29 |
Correct |
5 ms |
6528 KB |
Output is correct |
30 |
Correct |
5 ms |
6528 KB |
Output is correct |
31 |
Correct |
5 ms |
6400 KB |
Output is correct |
32 |
Correct |
5 ms |
6400 KB |
Output is correct |
33 |
Correct |
5 ms |
6400 KB |
Output is correct |
34 |
Correct |
5 ms |
6400 KB |
Output is correct |
35 |
Correct |
14 ms |
7424 KB |
Output is correct |
36 |
Correct |
6 ms |
6528 KB |
Output is correct |
37 |
Correct |
18 ms |
7680 KB |
Output is correct |
38 |
Correct |
14 ms |
8320 KB |
Output is correct |
39 |
Correct |
108 ms |
17608 KB |
Output is correct |
40 |
Correct |
132 ms |
17528 KB |
Output is correct |
41 |
Correct |
60 ms |
17528 KB |
Output is correct |
42 |
Correct |
104 ms |
17528 KB |
Output is correct |
43 |
Correct |
128 ms |
22000 KB |
Output is correct |
44 |
Correct |
50 ms |
17528 KB |
Output is correct |