#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-1,sy-1,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-1) && y == (ey-1)){
cout << dist;
return 0;
}
auto je = visited.find({x,y});
if (je == visited.end()){
visited.insert({x,y});
for (int i = 0; i < dirs.size(); i++){
ll nx,ny;
nx = x + dirs[i][0];
ny = y + dirs[i][1];
if (ny >= 0 && ny < n){
if (nx < 0){ // Toto se mohlo stát jen při pohybu doleva
ny -= 1;
if (ny >= 0 && ny < n){
nx = lines[ny];}
}
else if (nx > lines[ny]){
if (dirs[i][1] == 0){
nx = 0;
ny ++;
}
else{
if (ny >= 0 && ny < n){
nx = lines[ny];
}
}
}
if (ny >= 0 && ny < n && nx >= 0 && nx <= lines[ny]){
fronta.push({nx, ny, dist + 1});
}
}
}
}
}
cout << 0;
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... |