#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mmod = 998244353;
#define vl vector<long long>
#define vll vector<vector<long long>>
ll pow(ll x, ll n, ll mod){
if (n == 0){
return 1;
}
ll half = pow(x, n / 2, mod);
ll half_square = (half * half) % mod;
if (n % 2 == 0) {
return half_square;
} else {
return (half_square * x) % mod;
}
}
ll inversion_x(ll x, ll m){
ll vysledek = pow(x,m-2);
return vysledek;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
ll sx,sy;
ll ex,ey;
cin >> n;
cin >> sy >> sx;
cin >> ey >> ex;
vl lines;
for (ll i = 0; i<n; i++){
ll num;
cin >> num;
lines.push_back(num);
}
queue<vl> fronta;
fronta.push({sx,sy,0});
set<vl> visited;
vll dirs {{1,0},{-1,0},{0,1},{0,-1}};
while (fronta.size() > 0){
vl prvek = fronta.front();
fronta.pop();
ll x = prvek[0];
ll y = prvek[1];
ll dist = prvek[2];
if (x == ex && y == ey){
cout << dist;
return 0;
}
for (ll i = 0; i < dirs.size(); i++){
ll nx,ny;
nx = x + dirs[i][0];
ny = y + dirs[i][1];
if (nx < 0){
ny -= 1;
nx = lines[ny];
}
if (nx > lines[ny]){
nx = 0;
ny ++;
}
if (y >= 0 && y < n){
auto exist = visited.find({nx,ny});
if (exist == visited.end()){
visited.insert({nx,ny});
fronta.push({nx, ny, dist + 1});
}
}
}
}
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... |