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>
#define div /
#define ll long long
#define fore(i, l, r) for(int i=int(l); i<int(r); i++)
#define sz(a) int((a).size())
using namespace std;
const int INF = 1e9;
const int MX = 5e5 + 23;
const int MOD = 1000000007;
const int MAX_N = 5e5+23;
const int N = 1e6;
int grid[1005][5005],dist[1005][5005],visited[1005][5005],oznacen[1005][5005];
void solve() {
int n; cin >> n;
int sl,sc,el,ec;
cin >> sl >> sc >> el >> ec;
int l[n];
memset(grid,0,sizeof grid);
fore(i,1,n+1) {
cin >> l[i];
fore(j,1,l[i]+1)
grid[i][j] = 1;
}
fore(i,0,1003)
fore(j,0,5003)
dist[i][j] = INF;
queue<pair<int,int>> q;
dist[sl][sc] = 0;
visited[sl][sc] = 1;
q.push({sl,sc});
while(!q.empty()) {
pair<int,int> s=q.front(); q.pop();
int SL = s.first, SC = s.second;
if(SL+1 <= n+1) {
dist[SL+1][SC] = min(dist[SL+1][SC], dist[SL][SC] + grid[SL+1][SC]);
if(!visited[SL+1][SC])
q.push({SL+1,SC});
visited[SL+1][SC] = 1;
}
if(SL-1 >= 0) {
dist[SL-1][SC] = min(dist[SL-1][SC], dist[SL][SC] + grid[SL-1][SC]);
if(!visited[SL-1][SC])
q.push({SL-1,SC});
visited[SL-1][SC] = 1;
}
if(SC+1 <= 5001) {
dist[SL][SC+1] = min(dist[SL][SC+1], dist[SL][SC] + grid[SL][SC+1]);
if(!visited[SL][SC+1])
q.push({SL,SC+1});
visited[SL][SC+1] = 1;
}
if(SC-1 >= 0) {
dist[SL][SC-1] = min(dist[SL][SC-1], dist[SL][SC] + grid[SL][SC-1]);
if(!visited[SL][SC-1])
q.push({SL,SC-1});
visited[SL][SC-1] = 1;
}
if(SC == 1 and SL > 1 and SL <= n) { // SPECIAL LEFT PRESS
dist[SL-1][l[SL-1]] = min(dist[SL-1][l[SL-1]], dist[SL][SC] + 1);
if(!oznacen[SL-1][l[SL-1]])
q.push({SL-1,l[SL-1]});
oznacen[SL-1][l[SL-1]] = 1;
}
if(SC == l[SL-1] and SL < n) { // SPECIAL RIGHT PRESS
dist[SL+1][1] = min(dist[SL+1][1], dist[SL][SC] + 1);
if(!oznacen[SL+1][1])
q.push({SL+1, 1});
oznacen[SL+1][1] = 1;
}
}
cout << dist[el][ec] << endl;
}
int main() {
ios::sync_with_stdio(false);
int t=1;
while(t--) solve();
}
# | 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... |