이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
int arr[n + 1];
long long int dp[n + 2][5002];
long long int vis[n + 2][5002];
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= 5000; j++){
dp[i][j] = 1e9;
vis[i][j] = 0;
}
}
queue<pair<int,int>>q;
q.push({x1,y1});
dp[x1][y1] = 0;
vis[x1][y1]++;
while(!q.empty()){
pair<int,int>v = q.front();
q.pop();
int x = v.first;
int y = v.second;
if(x == x2 and y == y2){
cout << dp[x][y];
return 0;
}
int nx,ny;
if(y != (arr[x] + 1)){
nx = x;
ny = y + 1;
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}else{
if(x != n){
nx = x + 1;
ny = 1;
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}
}
if(y > 0){
nx = x;
ny = y - 1;
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}else{
if(x != 1){
nx = x - 1;
ny = arr[x - 1] + 1;
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}
}
if(x != 1){
nx = x - 1;
ny = min(arr[x - 1] + 1, y);
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}
if(x != n){
nx = x + 1;
ny = min(arr[x + 1] + 1, y);
if(vis[nx][ny] == 0){
dp[nx][ny] = dp[x][y] +1;
q.push({nx,ny});
vis[nx][ny]++;
}
}
}
}
# | 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... |