#include <bits/stdc++.h>
#define int long long
#define double long double
#define pii pair <int,int>
#define tiii tuple <int, int, int>
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub(a, b) upper_bound(a.begin(), a.end(), b) - a.begin()
#define lb(a, b) lower_bound(a.begin(), a.end(), b) - a.begin()
#define ve vector
#define graph(a, n) vector <int> a[n];
#define wgraph(a, n) vector <pii> a[n];
#define emb emplace_back
#define em emplace
#define ins insert
#define er erase
#define iShowSpeed cin.tie(NULL)->sync_with_stdio(false)
using namespace std;
template <typename T>
using greater_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1e9 + 7;
const int inf = 1e18;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int n, m;
bool inside(int x, int y) {
return x > 0 && x <= n && y > 0 && y <= m;
}
int dir(char c) {
if (c == 'E') return 0;
if (c == 'S') return 1;
if (c == 'W') return 2;
if (c == 'N') return 3;
if (c == 'X') return -1;
}
int32_t main(){
iShowSpeed;
cin >> n >> m;
vector <vector <int>> a(n + 1, vector <int> (m + 1));
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
char c; cin >> c;
a[i][j] = dir(c);
}
vector <vector <int>> dis(n + 1, vector <int> (m + 1, inf)); dis[1][1] = 0;
greater_priority_queue <tiii> q; q.emplace(0, 1, 1);
while (!q.empty()) {
auto [w, x, y] = q.top(); q.pop();
// cout << x << ", " << y << ": " << w << "\n";
if (w > dis[x][y] || a[x][y] == -1) continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[(a[x][y] + i) % 4], ny = y + dy[(a[x][y] + i) % 4];
if (!inside(nx, ny) || dis[nx][ny] <= dis[x][y] + i) continue;
dis[nx][ny] = dis[x][y] + i;
q.emplace(dis[nx][ny], nx, ny);
}
}
cout << (dis[n][m] == inf ? -1 : dis[n][m]);
}
컴파일 시 표준 에러 (stderr) 메시지
adventure.cpp: In function 'long long int dir(char)':
adventure.cpp:43:1: warning: control reaches end of non-void function [-Wreturn-type]
43 | }
| ^
# | 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... |