이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define s second
#define f first
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
#define int ll
using namespace std;
char A[505][505];
int dp[505][505],n,m;
bool f[505][505];
priority_queue <pair<int,pi> > q;
vector <char> v = {'N','E','S','W'};
int go(char x,char y){
if (x == 'X') return 1e9;
int lf = 0,rg = 0;
for (int i = 0; i < v.size(); i++){
if (v[i] == x) lf = i;
if (v[i] == y) rg = i;
}
if (lf <= rg) return rg - lf;
return 4 - lf + rg;
}
bool check(int x,int y){
if (x < 0 || x >= n || y < 0 || y >= m)
return 0;
return 1;
}
signed main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> A[i][j],dp[i][j] = 1e9;
q.push({0,{0,0}});
dp[0][0] = 0;
while(q.size()){
int x = q.top().s.f,y = q.top().s.s,d = -q.top().f;
q.pop();
if (dp[x][y] != d) continue;
if (check(x - 1,y)){
int a = d + go(A[x][y],'N');
if (dp[x - 1][y] > a){
dp[x - 1][y] = a;
q.push({-a,{x - 1,y}});
}
}
if (check(x + 1,y)){
int b = d + go(A[x][y],'S');
if (dp[x + 1][y] > b){
dp[x + 1][y] = b;
q.push({-b,{x + 1,y}});
}
}
if (check(x,y - 1)){
int c = d + go(A[x][y],'W');
if (dp[x][y - 1] > c){
dp[x][y - 1] = c;
q.push({-c,{x,y - 1}});
}
}
if (check(x,y + 1)){
int k = d + go(A[x][y],'E');
if (dp[x][y + 1] > k){
dp[x][y + 1] = k;
q.push({-k,{x,y + 1}});
}
}
}
ll ans = dp[n - 1][m - 1];
if (ans >= 1e9) cout<<-1<<"\n";
else cout<<ans<<"\n";
}
컴파일 시 표준 에러 (stderr) 메시지
adventure.cpp: In function 'long long int go(char, char)':
adventure.cpp:21:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# | 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... |