#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 502;
const int xx[4] = {0, 1, 0, -1};
const int yy[4] = {1, 0, -1, 0};
const char cc[4] = {'E', 'S', 'W', 'N'};
int n, m;
char a[N][N];
struct ban
{
int x, y, d;
ban(){}
ban(int x, int y, int d)
{
this->x = x;
this->y = y;
this->d = d;
}
};
bool operator<(const ban& a, const ban& b)
{
return a.d > b.d;
}
bool c[N][N];
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> (a[i] + 1);
priority_queue<ban> q;
q.push(ban(n, m, 0));
while (!q.empty())
{
ban t;
do
{
if (q.empty())
{
cout << "-1\n";
return;
}
t = q.top();
q.pop();
} while (c[t.x][t.y]);
c[t.x][t.y] = true;
if (t.x == 1 && t.y == 1)
{
cout << t.d << "\n";
return;
}
for (int i = 0; i < 4; ++i)
{
int hx = t.x + xx[i];
int hy = t.y + yy[i];
if (1 <= hx && hx <= n && 1 <= hy && hy <= m)
{
int j = (i + 2) % 4;
int u = -1;
for (int k = 0; k < 4; ++k)
{
if (a[hx][hy] == cc[k])
{
u = k;
break;
}
}
if (u != -1)
{
int dd;
if (u <= j)
dd = j - u;
else
dd = j + 4 - u;
q.push(ban(hx, hy, t.d + dd));
}
}
}
}
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 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 |
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 |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |