//Awesome Arrowland Adventure
#define _CTR_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <utility>
#include <functional>
typedef long long int ll;
enum Kier
{
N, E, S, W, M,
};
template <typename T> T in()
{
T x;
std::cin >> x;
return x;
}
Kier in()
{
char c(in<char>());
switch (c)
{
case 'N':
return N;
break;
case 'E':
return E;
break;
case 'S':
return S;
break;
case 'W':
return W;
break;
default:
return M;
break;
}
}
inline Kier ruch(Kier z, Kier d)
{
return static_cast<Kier>((M + d - z) % M);
}
constexpr int maxn = 503;
int odp[maxn][maxn];
Kier arr[maxn][maxn];
int main()
{
std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios_base::sync_with_stdio(0);
int n(in<int>()), m(in<int>());
for (int i(0); i < n; ++i)for (int j(0); j < m; ++j)
{
Kier k(in());
if (k == M) { if (i < n - 1 || j < m - 1)odp[i][j] = 1; }
else arr[i][j] = k;
}
if (odp[0][0])
{
std::cout << -(n > 1 || m > 1) << '\n';
return 0;
}
std::priority_queue <std::pair <int, std::pair <int, int>>> kul;
kul.push({ -1,{ n - 1, m - 1 } });
while (kul.size())
{
std::function <bool(int x, int y)> ok([n, m](int x, int y) -> bool
{
if (x < 0 || y < 0 || x >= n || y >= m)return 0;
return !odp[x][y];
});
int x(kul.top().second.first);
int y(kul.top().second.second);
int odl(kul.top().first);
kul.pop();
if (odp[x][y])continue;
odp[x][y] = odl;
if (ok(x - 1, y))kul.push({odl - ruch(arr[x - 1][y], S), {x - 1, y} });
if (ok(x + 1, y))kul.push({odl - ruch(arr[x + 1][y], N), {x + 1, y} });
if (ok(x, y - 1))kul.push({odl - ruch(arr[x][y - 1], E), {x, y - 1} });
if (ok(x, y + 1))kul.push({odl - ruch(arr[x][y + 1], W), {x, y + 1} });
}
std::cout << -odp[0][0] - 1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 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 |
204 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 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 |
204 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 |
204 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 |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
7 ms |
716 KB |
Output is correct |
38 |
Correct |
2 ms |
1356 KB |
Output is correct |
39 |
Correct |
67 ms |
2564 KB |
Output is correct |
40 |
Correct |
67 ms |
2560 KB |
Output is correct |
41 |
Correct |
68 ms |
2540 KB |
Output is correct |
42 |
Correct |
66 ms |
2560 KB |
Output is correct |
43 |
Correct |
89 ms |
5432 KB |
Output is correct |
44 |
Correct |
7 ms |
2508 KB |
Output is correct |