#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<pair<ll, ll>>
#define vpp vector<pair<ll, pair<ll, ll>>>
#define pp pair<ll, ll>
#define qi queue<ll>
#define qp queue<pp>
#define pqi priority_queue<ll>
#define pqp priority_queue<pp>
#define mi map<ll, ll>
#define mpi map<pp, ll>
#define mip map<ll, pp>
#define mp map<pp, pp>
#define mb map<ll, bool>
#define si set<ll>
#define sp set<pp>
#define sc set<char>
#define mod 1000000007
#define inf INT_MAX
#define all(x) (x).begin(), (x).end()
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin >> n;
ll sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
sx--; sy--; ex--; ey--;
vi a(n);
for(int i = 0; i < n; i++) cin >> a[i];
vvi g(n);
for(int i = 0; i < n; i++) {
vi line(a[i] + 1, inf);
g[i] = line;
}
g[sx][sy] = 0;
priority_queue<pair<ll, pp>> q;
q.push({0, {sx, sy}});
vp dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
while(q.size()) {
ll cur = -q.top().first;
auto[x, y] = q.top().second;
q.pop();
if(g[x][y] < cur) continue;
for(auto[dirX, dirY] : dir) {
ll newX = x + dirX, newY = y + dirY;
if(dirY != 0) {
if(newY < 0) {
newY = inf;
newX--;
}
else if(newY > a[newX]) {
newY = 0;
newX++;
}
}
if(newX < 0 || newX >= n) continue;
newY = min(newY, a[newX]);
if(g[newX][newY] > cur + 1) {
g[newX][newY] = cur + 1;
q.push({-(cur + 1), {newX, newY}});
}
}
}
cout << g[ex][ey] << '\n';
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... |