#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, ll>
#define fi first
#define se second
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int> > board(1 + n + 1, vector<int> (1 + m + 1, -1));
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
char ch;
cin >> ch;
if(ch == 'N') board[i][j] = 0;
if(ch == 'E') board[i][j] = 3;
if(ch == 'S') board[i][j] = 2;
if(ch == 'W') board[i][j] = 1;
}
}
vector<vector<vector<pair<int, pair<int, int> > > > > graph(1 + n + 1, vector<vector<pair<int, pair<int, int> > > > (1 + m + 1));
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
// cout << i << " " << j << ":\n";
if(board[i][j] == -1) continue;
graph[i][j].pb(mp((board[i][j] + 0) % 4, mp(i - 1, j)));
graph[i][j].pb(mp((board[i][j] + 1) % 4, mp(i, j + 1)));
graph[i][j].pb(mp((board[i][j] + 2) % 4, mp(i + 1, j)));
graph[i][j].pb(mp((board[i][j] + 3) % 4, mp(i, j - 1)));
// for(auto p : graph[i][j]) cout << "{" << p.fi << " {" << p.se.fi << " " << p.se.se << "} }\n";
}
}
vector<vector<bool> > done(1 + n + 1, vector<bool> (1 + m + 1, false));
vector<vector<int> > dist(1 + n + 1, vector<int> (1 + m + 1, 0));
priority_queue<pair<int, pair<int, int> > > q;
q.push(mp(0, mp(1, 1)));
while(!q.empty()) {
auto cur = q.top();
q.pop();
int i = cur.se.fi;
int j = cur.se.se;
int d = -cur.fi;
if(!done[i][j]) {
done[i][j] = true;
dist[i][j] = d;
for(auto nei : graph[i][j]) {
if(!done[nei.se.fi][nei.se.se]) {
q.push(mp(-(d + nei.fi), mp(nei.se.fi, nei.se.se)));
}
}
}
}
if(!done[n][m]) cout << "-1\n";
else cout << dist[n][m] << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
11 ms |
2240 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
13 ms |
2512 KB |
Output is correct |
38 |
Correct |
9 ms |
3780 KB |
Output is correct |
39 |
Correct |
139 ms |
21060 KB |
Output is correct |
40 |
Correct |
131 ms |
21108 KB |
Output is correct |
41 |
Correct |
48 ms |
21000 KB |
Output is correct |
42 |
Correct |
129 ms |
21120 KB |
Output is correct |
43 |
Correct |
135 ms |
27232 KB |
Output is correct |
44 |
Correct |
58 ms |
21004 KB |
Output is correct |