이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 (1)
{
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |