This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |