#include <bits/stdc++.h>
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define oprecision \
cout.precision(30); \
cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define mp(x, y) make_pair(x, y)
#define F first
#define S second
using namespace std;
int n, m, dp[5005][5005];
char a[5005][5005];
map<char, map<char, int>> cost;
priority_queue<pair<int, pair<int, int>>> s;
bool ok(int i, int j) { return ((1 <= i && i <= n) && (1 <= j && j <= m)); }
int main() {
speed;
oprecision;
// code
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
dp[i][j] = 1e9;
}
}
dp[1][1] = 0;
cost['W']['N'] = cost['N']['E'] = cost['E']['S'] = cost['S']['W'] = 1;
cost['W']['E'] = cost['N']['S'] = cost['E']['W'] = cost['S']['N'] = 2;
cost['W']['S'] = cost['N']['W'] = cost['E']['N'] = cost['S']['E'] = 3;
cost['X']['S'] = cost['X']['N'] = cost['X']['E'] = cost['X']['W'] = 1e9;
s.push(mp(0, mp(1, 1)));
while (sz(s)) {
int i = s.top().S.F, j = s.top().S.S;
s.pop();
if (ok(i - 1, j) && dp[i - 1][j] > dp[i][j] + cost[a[i][j]]['N']) {
dp[i - 1][j] = dp[i][j] + cost[a[i][j]]['N'];
s.push(mp(-dp[i - 1][j], mp(i - 1, j)));
}
if (ok(i + 1, j) && dp[i + 1][j] > dp[i][j] + cost[a[i][j]]['S']) {
dp[i + 1][j] = dp[i][j] + cost[a[i][j]]['S'];
s.push(mp(-dp[i + 1][j], mp(i + 1, j)));
}
if (ok(i, j - 1) && dp[i][j - 1] > dp[i][j] + cost[a[i][j]]['W']) {
dp[i][j - 1] = dp[i][j] + cost[a[i][j]]['W'];
s.push(mp(-dp[i][j - 1], mp(i, j - 1)));
}
if (ok(i, j + 1) && dp[i][j + 1] > dp[i][j] + cost[a[i][j]]['E']) {
dp[i][j + 1] = dp[i][j] + cost[a[i][j]]['E'];
s.push(mp(-dp[i][j + 1], mp(i, j + 1)));
}
}
cout << (dp[n][m] == 1e9 ? -1 : dp[n][m]);
// endl
#ifndef ONLINE_JUDGE
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
404 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
392 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
392 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
404 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
392 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
396 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
392 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
9 ms |
896 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
12 ms |
1408 KB |
Output is correct |
38 |
Correct |
3 ms |
2688 KB |
Output is correct |
39 |
Correct |
91 ms |
5924 KB |
Output is correct |
40 |
Correct |
94 ms |
6008 KB |
Output is correct |
41 |
Correct |
9 ms |
5928 KB |
Output is correct |
42 |
Correct |
124 ms |
5888 KB |
Output is correct |
43 |
Correct |
146 ms |
9064 KB |
Output is correct |
44 |
Correct |
9 ms |
5888 KB |
Output is correct |