이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxN = 505;
char str[mxN][mxN];
ll dist[mxN][mxN];
string pos = "NESW";
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool check(int i, int j, int n, int m) {
return i >= 0 && j >= 0 && i < n && j < m;
}
int main() {
int n, m;
scanf("%d %d\n", &n, &m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> str[i][j];
dist[i][j] = LLONG_MAX;
}
}
// cout << str[2][2] << "\n";
dist[0][0] = 0;
// (distance, i, j)
priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll> >, greater<tuple<ll, ll, ll> > > pq;
pq.push({0, 0, 0});
while(!pq.empty()) {
ll i = get<1>(pq.top()), j = get<2>(pq.top()), w = get<0>(pq.top());
// cout << i << " " << j << " " << w << "\n";
pq.pop();
if(dist[i][j] != w) continue;
if(str[i][j] == 'X') continue;
ll direction = pos.find(str[i][j]);
// cout << str[i][j] << " " << direction << "\n";
for(int k = 0; k < 4; k++) {
int x = i+dx[k], y = j+dy[k];
// cout << x << " " << y << "\n";
if(check(x, y, n, m)) {
ll weight = (k-direction+4)%4;
// cout << dist[x][y] << " " << dist[i][j] << " " << weight << "\n";
if(dist[x][y] > dist[i][j]+weight) {
dist[x][y] = dist[i][j]+weight;
pq.push({dist[x][y], x, y});
}
}
}
}
if(dist[n-1][m-1] == LLONG_MAX)
printf("-1\n");
else
printf("%lld\n", dist[n-1][m-1]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
adventure.cpp: In function 'int main()':
adventure.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d %d\n", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |