Submission #1300544

#TimeUsernameProblemLanguageResultExecution timeMemory
1300544iq500Text editor (CEOI24_editor)C++20
0 / 100
550 ms1114112 KiB
#include <bits/stdc++.h>
#define fir first
#define sec second
using namespace std;

int n;
int sr, sc, er, ec; 
vector<vector<int>> a;
vector<vector<bool>> v;
queue<pair<int, int>> q;

void bfs(){
    q.push({sr, sc});
    v[sr][sc]=1;
    a[sr][sc]=0;
    while(q.size()){
        int x=q.front().fir, y=q.front().sec, d=a[x][y]+1;
        q.pop();
        //yukarı
        if(x>1){
            
            int nw=a[x-1].size()-1;
            if(nw+1<=y && !v[x-1][nw]){
                v[x-1][nw]=1;
                a[x-1][nw]=d;
                q.push({x-1, nw});
            }
            else if(nw+1>y && !v[x-1][y]){
                v[x-1][y]=1;
                a[x-1][y]=d;
                q.push({x-1, y});
            }
        }
        //aşağı
        if(x<n){
            
            int nw=a[x+1].size()-1;
            if(nw+1<=y && !v[x+1][nw]){
                v[x+1][nw]=1;
                a[x+1][nw]=d;
                q.push({x+1, nw});
            }
            else if(nw+1>y && !v[x+1][y]){
                v[x+1][y]=1;
                a[x+1][y]=d;
                q.push({x+1, y});
            }
        }
        //sağa
        if(y<a[x].size()-1){
            if(!v[x][y+1]){
                v[x][y+1]=1;
                a[x][y+1]=d;
                q.push({x, y+1});
            }
        }
        else{
            //bir alt satıra geççez
            if(x<n && !v[x+1][1]){
                v[x+1][1]=1;
                a[x+1][1]=d;
                q.push({x+1, 1});
            }
        }
        //sola
        if(y>1){
            if(!v[x][y-1]){
                v[x][y-1]=1;
                a[x][y-1]=d;
                q.push({x, y-1});
            }
        }
        else{
            if(x>1 && !v[x-1][a[x-1].size()-1]){
                v[x-1][a[x-1].size()-1]=1;
                a[x-1][a[x-1].size()-1]=d;
                q.push({x-1, a[x-1].size()-1});
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    cin>>sr>>sc>>er>>ec;
    //s-row, s-col, e-row, e-col
    a.resize(n+1);
    v.resize(n+1);
    for(int i=1; i<=n; i++){
        int x; cin>>x;
        a[i].resize(x+2);
        v[i].resize(x+2, 0);
    }
    bfs();
    cout<<a[er][ec];



    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...