This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
// author : Pluton
#define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
#define ll long long
#define pb push_back
#define arr array
#define fi first
#define se second
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define all(a) a.begin(), a.end()
#define pii pair<int,int>
using namespace std;
const int INF = 1e18;
struct BIT {
int n;
vector<int> ft;
void init(int N) {
n = N + 5;
ft.assign(n + 5, 0);
}
void add(int pos, int val) {
for (pos = pos + 1; pos <= n; pos += pos & -pos) ft[pos] += val;
}
int get(int pos, int res = 0) {
for (pos = pos + 1; pos > 0; pos -= pos & -pos) res += ft[pos];
return res;
}
};
map<char, vector<vector<int>>> d;
int n, m;
bool f(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m) {
return true;
}
return false;
}
const int N = 505;
vector<vector<char>> g;
vector<vector<int>> cost;
int bfs() {
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
q.push({0, 0, 0});
cost[0][0] = 0;
while (!q.empty()) {
vector<int> u = q.top();
q.pop();
auto move = d[g[u[2]][u[1]]];
for (auto e : move) {
int x = e[0] + u[1];
int y = e[1] + u[2];
if (f(x, y) && cost[y][x] > cost[u[2]][u[1]] + e[2]) {
cost[y][x] = cost[u[2]][u[1]] + e[2];
q.push({cost[y][x], x, y});
}
}
}
return cost[m - 1][n - 1];
}
void _() {
d['N'] = {{0, -1, 0}, {1, 0, 1}, {0, 1, 2}, {-1, 0, 3}};
d['E'] = {{0, -1, 3}, {1, 0, 0}, {0, 1, 1}, {-1, 0, 2}};
d['S'] = {{0, -1, 2}, {1, 0, 3}, {0, 1, 0}, {-1, 0, 1}};
d['W'] = {{0, -1, 1}, {1, 0, 2}, {0, 1, 3}, {-1, 0, 0}};
d['X'] = {};
cin >> m >> n;
for (int i = 0; i < m; ++i) {
g.push_back({});
cost.push_back({});
for (int j = 0; j < n; ++j) {
char c;
cin >> c;
g[i].push_back(c);
cost[i].push_back(INF);
}
}
int res = bfs();
cout << (res == INF ? -1 : res);
}
signed main() {
OPT
int tc = 1;
while (tc--) {
_();
}
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... |