이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve() {
int n, sl, sc, el, ec;
cin >> n >> sl >> sc >> el >> ec;
int l[n];
for(int i=0; i<n; ++i) cin >> l[i];
priority_queue<pair<int,pair<int,int>>>pq;
vector<vector<int>>dp(n, vector<int>(5001, INT_MAX));
vector<vector<bool>>visited(n, vector<bool>(5001, 0));
--sl, --sc, --el, --ec;
dp[sl][sc] = 0;
pq.push({0, {sl, sc}});
while(!pq.empty()) {
int x = pq.top().second.first, y = pq.top().second.second;
pq.pop();
if(visited[x][y]) continue;
visited[x][y] = 1;
if(x != n-2) { // down
int new_y = y;
new_y = min(new_y, l[x+1] - 1);
if(dp[x+1][new_y] > dp[x][y] + 1) {
dp[x+1][new_y] = dp[x][y] + 1;
pq.push({-dp[x+1][new_y], {x+1, new_y}});
}
}
if(x) { // up
int new_y = y;
new_y = min(new_y, l[x-1] - 1);
if(dp[x-1][new_y] > dp[x][y] + 1) {
dp[x-1][new_y] = dp[x][y] + 1;
pq.push({-dp[x-1][new_y], {x-1, new_y}});
}
}
if(!y && x) { // left up
int new_x = x - 1, new_y = l[new_x] - 1;
if(dp[x][y] + 1 < dp[new_x][new_y]) {
dp[new_x][new_y] = dp[x][y] + 1;
pq.push({-dp[new_x][new_y], {new_x, new_y}});
}
} else if(y) { // left
if(dp[x][y] + 1 < dp[x][y-1]) {
dp[x][y-1] = dp[x][y] + 1;
pq.push({-dp[x][y-1], {x, y-1}});
}
}
if(y == l[x] - 1 && x != n-2) { // right down
int new_x = x+1, new_y = 0;
if(dp[x][y] + 1 < dp[new_x][new_y]) {
dp[new_x][new_y] = dp[x][y] + 1;
pq.push({-dp[new_x][new_y], {new_x, new_y}});
}
} else if(y != l[x] - 1) { // right
if(dp[x][y] + 1 < dp[x][y+1]) {
dp[x][y+1] = dp[x][y] + 1;
pq.push({-dp[x][y+1], {x, y+1}});
}
}
}
cout << dp[el][ec] << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while(t--) solve();
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |